Reputation: 581
I am using the laravel
polymorphic relation.
cartable model:
public function objectable()
{
return $this->morphTo();
}
instructions model:
public function cartable()
{
return $this->morphMany('PTA_OIMS\Cartable', 'objectable');
}
reports model:
public function reports()
{
return $this->morphMany('PTA_OIMS\Cartable', 'objectable');
}
My instruction table has extra related table called instruction_logs.
But in the reports table, this relationship does not exist. When my object is report this query has error. Because the reports.report_logs does not exist.
$data = Cartable::where('id', '=', $cartableID)
->with('objectable')
->with('objectable.objectable_logs.attachment') //line error
->with('box.position')
->with('box.assign.staff')
->first();
return $data;
How to handle query if objectable_logs did not exist skip the line error?
Upvotes: 1
Views: 478
Reputation: 1213
$data = Cartable::with([
'objectable',
'objectable.objectable_logs.attachment',
'box.position',
'box.assign.staff'
])->find($cartableID);
return $data;
if attachment
is a column in objectable_logs
table, it should be like:
$data = Cartable::with([
'objectable',
'objectable.objectable_logs:objectable_id,attachment',
'box.position',
'box.assign.staff'
])->find($cartableID);
return $data;
Upvotes: 2