F
F
frontendo2017-02-06 20:59:48
Laravel
frontendo, 2017-02-06 20:59:48

How to use one to one relationship with Eloquent collections?

$user = Auth::user();
$project = $user->roles()->first()->project()->first();
we get the current user, then we get the user's roles in various projects, select the first role and get the project where the user has a role.
and so a question in what, how in one request to receive a collection of all projects where the user has roles?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
frontendo, 2017-02-06
@frontendo

after a short dive into the documentation, I found the solution
$projects = $user->roles()->get()->transform(function($item){
return $item->project()->first();
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question