亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel Eloquent 在我的搜索系統請求中使用 orWhere 查詢

Laravel Eloquent 在我的搜索系統請求中使用 orWhere 查詢

PHP
料青山看我應如是 2022-07-09 10:40:21
我試圖用搜索系統編寫一個請求。這里的代碼:$search = request()->get('search');if(Auth::user()->hasRole('admin') || true) {  list($orderBy, $orderDirection) = explode('.', request()->get('sort_by'));  $prestations = Prestation::with(    'service:id,name',    'facility:id,name'  )  ->orWhere('service:name', 'regexp', "/$search/i")  ->orderBy($orderBy, $orderDirection)  ->simplePaginate(50);  $res = [    'results' => $prestations,    'total' => Prestation::all()->count(),  ];  return $res;}問題是我不知道如何做一些我嘗試過的事情orWhere-> 獲取所有服務名稱(來自關系“與”),它們等于我的$search.
查看完整描述

1 回答

?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

試試這個查詢。


$prestations = Prestation::with(

                [

                'service' => function($service) use($search){

                    $service->select(['id','name'])->where('name', $search);

                },

                'facility' => function($facility) {

                    $facility->select(['id','name'])

                }

                ]

            );


查看完整回答
反對 回復 2022-07-09
  • 1 回答
  • 0 關注
  • 138 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號