LEFT JOIN com AND no Eloquent Laravel 5

Asked

Viewed 7,065 times

4

I have a query with Left Join with AND I’m not able to implement it in Laravel 5.

A snippet of the query:

LEFT JOIN visitante v ON a.codigo = v.id 
AND v.data BETWEEN '2015-06-01' and '2016-05-31' and v.status = '1'

I tried it but it didn’t work:

->leftJoin(DB::raw('visitante v ON a.codigo = v.id
AND v.data BETWEEN \'2015-06-01\' and \'2016-05-31\' and v.status = \'1\''))
  • Is there a mistake? Because it doesn’t work?

  • @Virgilionovic Error appears: Missing argument 2 for Illuminate Database Query Builder::leftJoin(), called in /...Controller.php on line 120 and defined which is on this line

  • The way this query is, if you can use the simplest way by doing all the SQL and going to run, so it won’t work because leftJoin does not use DB::Raw (so far as I know), then do https://laravel.com/docs/5.3/database#running-queries as described on that link. Also has little information to develop an exact answer!

  • It is I had already seen and not have much information I will continue looking on the Net

1 answer

5


I managed to solve it this way!

->leftJoin('visitante as v', function($join) 
            {

                $join->on('a.codigo', '=', 'v.id');

                $join->on('v.data','>=',DB::raw("'2015-06-01'"));
                $join->on('v.data','<=',DB::raw("'2016-05-31'"));
                $join->on('v.status','=',DB::raw("'1'"));

            })

Browser other questions tagged

You are not signed in. Login or sign up in order to post.