N
N
Northern Lights2019-10-31 11:49:51
Laravel
Northern Lights, 2019-10-31 11:49:51

How to select models from multiple tables?

Docks about communications read, but it is not clear.
There is an entity/table Document.
There is an entity/table DocumentType - in fact, this is just a reference.
Document has a foreign key on the table DocumentType
I tried to make a select:

return $this->model
            ->with('document_types')
            ->orderBy($orderBy, $sort)
            ->paginate($paged);

But an error occurs: Call to undefined relationship [document_types] on model [App\Models\Docflow\Document].
I thought in the Document model to declare a relationship:
public function document_types()
    {
        return $this->hasOne(DocumentType::class, 'type_id', 'id');
    }

got this:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'document_types.type_id' in 'where clause' (SQL: select * from `document_types` where `document_types`.`type_id` in (1, 3, 5, 18, 19, 20, 21, 22, 23, 24))
And it dawned on me that the Document->DocumentType relationship is most likely not a relationship that is described in the documentation. It's just a reference to the directory, and the directory doesn't know anything about the entity. I'm right?
If so, how to handle such situations, I would like to have a DocumentType object in the view from the document object.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad, 2019-10-31
@php666

Думаю вы просто перепутали аргументы:
public function document_types()
{
    return $this->hasOne(DocumentType::class, 'id', 'type_id');
}

PS I think the communication method itself can be called simply type

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question