为了账号安全,请及时绑定邮箱和手机立即绑定

Laravel:雄辩的orderBy hasOne关系列与with一起使用

Laravel:雄辩的orderBy hasOne关系列与with一起使用

PHP
扬帆大鱼 2021-05-06 18:23:33
我有一个Orders有hasOne关系的模型participant。public function participant(){    return $this->hasOne('App\OrderParticipant', 'order_id');}我需要检索Orders按以下排序的集合participant.last_name我的方法$orders = \App\Orders::with(['participant',])              ->where('user_id', $user->id)                ->orderBy('participant.last_name')              ->get();失败:未定义的表:7错误:缺少表\“参与者\” \ nLINE的FROM子句条目:... 1收集后我试图对它进行排序return $orders->sortBy('participant.last_name');但这根本没有排序顺便说一句我正在使用postgres
查看完整描述

3 回答

?
互换的青春

TA贡献1797条经验 获得超6个赞

您可以通过实现


  //  eager loading

  $orders = \App\Orders::with(['participant'=> function($q){

             $q->orderBy('last_name', 'asc/desc');

            }

           ])->get();


查看完整回答
反对 回复 2021-05-14
  • 3 回答
  • 0 关注
  • 469 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信