B
B
betrachter2021-01-09 17:22:28
Laravel
betrachter, 2021-01-09 17:22:28

How to access relationships from another table in Laravel?

Hello, please help. There is such structure of tables.
5ff9b9babae96394230004.png
On the page, I display data from the residents table. How to display the list of roles from the roles table in the same table?
The keys in the tables are created, the links in the models are registered.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
betrachter, 2021-01-09
@betrachter

In general, we managed to solve the problem. If anyone is interested:
in the controller:

$residents = Resident::with('user.roles')->paginate(25);

in the view:
@foreach($resident->user->roles as $role)
    {{$role->title}}
@endforeach

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question