I only want the relations to be loaded if they exist. Normally you use wouldhas()
.
This is a basic has()
:
Story::with('comments.user')->has('comments')->find($id);
in this case i only want the relation data if the comments relation has the data.
What i am trying to do is get data from 2 different relations:
Story::with('comments.user', 'tasks.comments.user')->find($id);
i only want the 'comments.user'
if comments
relation has any data and i only want the 'tasks.comments.user'
if the tasks
relation has data. Both are independent and don't rely on eachother. So if my comments
relation has data but my task
relation doesn't i still want to load the comments
relation data and vice versa.
Does anyone know the query for this ?
My story model:
public function comments()
{
return $this->morphMany('App\Tenant\Comment', 'commentable');
}
public function tasks()
{
return $this->hasMany('App\Tenant\Task');
}
My comments model:
public function commentable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo('App\Tenant\User');
}
My task model:
public function story()
{
return $this->belongsTo('App\Tenant\Story');
}
public function comments()
{
return $this->morphMany('App\Tenant\Comment', 'commentable');
}