如果关系不存在,则为默认值

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']);
}