Search code examples
mysqlsqllaravelleft-join

Laravel query join on not null


I have the following query join in MySQL.

left join the_fields as tf on 
  tf.tf_kode = x.tf_kode
  and tf.is_payed is not null

I tried to convert to Laravel Query Builder:

->leftJoin('the_fields as tf', function ($j) {
    $j->on('tf.tf_kode', '=', 'x.tf_kode');
    $j->on('tf.is_payed','!=', null);
})

But it shows the error unknown column '' on clause.


Solution

  • You can use any where filter method or ->on method:

    ->leftJoin('the_fields as tf', function ($j) {
        $j->on('tf.tf_kode', '=', 'x.tf_kode')->whereNotNull('tf.is_payed');
        // ->where(), ->whereBetween, ->whereIn() also works
    });