Using Laravel Datatables package: https://github.com/yajra/laravel-datatables
I am eager loading belongsTo
relationships. However, for some rows, the relationship might not exist, and the relationship_id
column would be null.
This is causing an issue with datatables:
{ data: 'relationship.name' },
It throws an error if the relationship doesn't exist. How would I set a default value for this particular column if the relationship was not found? I've tried using editColumn
, which worked for sorting, but not for searching.
You can use withDefault()
:
public function relationship()
{
return $this->belongsTo(...)->withDefault(['name' => 'default']);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With