douhe6181
2018-06-08 01:34
浏览 326
已采纳

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

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.

图片转代码服务由CSDN问答提供 功能建议

使用Laravel Datatables包: https://github.com/yajra/laravel-datatables

我渴望加载 belongsTo 关系。 但是,对于某些行,关系可能不存在, relationship_id 列将为null。

这会导致数据表出现问题:

  {data:'relationship.name'},
   
 
 

如果关系不存在,则会引发错误。 如果未找到关系,如何为此特定列设置默认值? 我尝试过使用 editColumn ,它可以用于排序,但不能用于搜索。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhang3822 2018-06-08 01:55
    已采纳

    You can use withDefault():

    public function relationship()
    {
        return $this->belongsTo(...)->withDefault(['name' => 'default']);
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题