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

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

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.

  • 写回答

1条回答 默认 最新

  • douzhang3822 2018-06-08 01:55
    关注

    You can use withDefault():

    public function relationship()
    {
        return $this->belongsTo(...)->withDefault(['name' => 'default']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?