編程學習網 > PHP技術 > laravel > laravel中使用with實現動態添加where條件
2020
02-06

laravel中使用with實現動態添加where條件

今天小編就為大家分享一篇在laravel中使用with實現動態添加where條件,具有好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
關鍵點:閉包

模型:

 
public function getCollect()
  {
    return $this->belongsTo('App\Components\Misc\Models\CollectCareerTalk', 'id', 'career_talk_id');
   }
  
public function otherMethod()
  {
    return $this->belongsTo('App\Components\Misc\Models\OtherMethodModel', '主鍵', '外鍵');
   }
 


倉庫: 

 
 
$this->model->with(['getCollect' => function ($q) use ($user_id) {
                      $q->where('user_id', $user_id);
                    },
                  'otherMethod'])
            ->select('id', 'title')
            ->where([
              'id' => 1
            ])
            ->first();
 


注意:with可以連多個表(數組形式傳參),沒有動態條件的,可以直接講模型方法名寫到with的參數中,有動態條件的,寫到閉包中

以上這篇在laravel中使用with實現動態添加where條件就是小編分享給大家的全部內容了

掃碼二維碼 獲取免費視頻學習資料

Python編程學習

查 看2022高級編程視頻教程免費獲取