Parameterized query binding in ON clause for a LEFT JOIN in Laravel Eloquent / Query Builder

后端 未结 3 843
孤城傲影
孤城傲影 2021-01-14 02:21

Let\'s say I want to show a full list of awards with type=\"color\":

Awards        Type     2013 Winner
======        ====     ===========
Blue Award    colo         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-14 02:39

    Currently you can use $join->where:

    $year = '2013';
    
    $awards = DB::table('awards')
             ->leftJoin('winners', 
                   function($join) use ($year)
                   {
                        $join
                            ->on('awards.id','=','winners.award_id')
                            // "where" instead of "on":
                            ->where('winners.year', '=', $year);
                   }
             ->where('awards.type','color')
             ->get();
    

提交回复
热议问题