N
N
Nikita Rubinkovskiy2018-02-18 16:58:01
Laravel
Nikita Rubinkovskiy, 2018-02-18 16:58:01

How to link two tables in Laravel?

Hello.
I have a table where values ​​from two columns need to be related to values ​​in one column from another table.
As I understand it, the many to many method is suitable, but it was not possible to implement it on my own.
How can this be done in Laravel?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex Wells, 2018-02-18
@Alex_Wells

Tyk

G
Gleb Nikolaev, 2018-02-18
@glebn

You need

hasOne(МодельСвязаннойТоблицы::class, 'название поля по которому связываем в нашей таблице', 'название поля по которому связываем в связываемой таблице')

For example
public function events(){
        return $this->hasOne(Events::class, 'city_id', 'id');
    }

In the official manual, it seems that this is explained in an accessible way using the example of posts and comments.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question