In join
, I am passing a direct value, and Laravel is trying to "read" this value as if it were a table.
function ($join) {
$join
->on('e.pee_fk_pes_codigo', '=', 'p.pes_codigo')
->on('e.pee_padrao', '=', '1');
});
In join
, I am passing a direct value, and Laravel is trying to "read" this value as if it were a table.
function ($join) {
$join
->on('e.pee_fk_pes_codigo', '=', 'p.pes_codigo')
->on('e.pee_padrao', '=', '1');
});
DB::Raw()
The class JoinClause
(used internally within Closure
, which you called in join
, by Laravel) by default expects value with the name of the fields of related tables.
To use a number, you can use DB::raw()
. So:
function ($join) {
$join
->on('e.pee_fk_pes_codigo', '=', 'p.pes_codigo')
->on('e.pee_padrao', '=', \DB::raw('1'));
})
DB:raw
will cause Laravel to accept an SQL expression to be passed as a parameter of on
.
Where
In another form, which I believe is the best one, you can also use the where
clause instead of on
:
function ($join) {
$join
->on('e.pee_fk_pes_codigo', '=', 'p.pes_codigo')
->where('e.pee_padrao', '=', '1');
})
See how the result is compiled into SQL:
$sqlString = Usuario::join('niveis', function ($j) {
$j->on('niveis.id', '=', 'usuarios.nivel_id')
->where('niveis.id', '=', 4);
})->toSql();
echo $sqlString;
Result:
select * from
'usuarios'
inner join
'niveis' on 'niveis'.'id' = 'usuarios'.'nivel_id'
and 'niveis'.'id' = ?