Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent

Is there a way to reference the base model i'm query against? $person doesn't exist yet because i'm defining it but I want to eager load constraint the contacts returned based on the person's primary department. How do I reference the instance of the person model?

Route::get('/test', function() {
	
	return $person = Person::with(['contacts' => function($q) use ($person) {
		$q->where('parent_entities_id', $person->parent_entities_id);
	}])->findOrFail(1);

});
Last updated 3 years ago.
0

Why can't you just create a new relationship method in your Person model ? Like :

public function parent(){
    return $this->belongsTo('Person', 'parent_entities_id');
}

And then just call:

return $person = Person::with('parent')->findOrFail(1);

Will it do the trick or maybe I am missunderstanding something ?

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

WebDevGirl webdevgirl Joined 20 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.