I have resources that have a privacy setting applied to them. Namely Community and Event. I created a morphOne relationship on all the resources that will require privacy like so
public function privacy(){
return $this->morphOne('App\Privacy', 'privacy_resource');
}
I am able to query the privacy setting for collections like so
$profile->events()->with('privacy')->get();
But when I attempt this on an individual resource like so
$event->with('privacy')->get();
I instead get a collection of all items from that resource.
I realize that I might be misusing get()
here and its actually getting all the events that have a privacy setting on them. Is there something other than get
that I should be using here? Thank you.
After you already retrieved the model, you can use load
instead of with
.
$event->load('privacy');
This is called Lazy Eager Loading.
In the statement $profile->events()->with('privacy')->get();
on the other hand, you did not yet retrieve the event models from the database. That is why you can use the with
method here, which will add a join to the query.