freez
freez

Reputation: 81

Eloquent nested relationship

i have a question to my db query.

My DB tables/schema:

i try to get all Projects where a user has access through customers to, with the following query:

//call
User::find(Auth::id())->first()->projects();

//User.model
public function projects() {
    return User::with('customers.projects')->get();
}

It works. But now i have User data, Customer data and Project Data in the result array. I want only the Projects. Is there an other way?

Upvotes: 3

Views: 168

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 220066

$user = auth()->user()->load('customers.projects');

$projects = $user->customers->pluck('projects')->collapse()->unique();

Upvotes: 0

Related Questions