-1

I'm using backpack CRUD for laravel and i'm trying to request my database. My problem is how can i join tables on values or null ?

this is what i want

  select * from `baskets`
   inner join `users` on `baskets`.`donor_id` = `users`.`id`
   inner join `users` as `ass` on `baskets`.`association_id` = `ass`.`id` or `baskets`.`association_id` is null
   where `users`.`_lft` >= 1 and `users`.`_rgt` <= 1000
   group by `baskets`.`basket_id`
   order by `baskets`.`basket_id` desc, `end_removal_date` desc

And this is what i'm trying to do with the query builder

   $this->crud->query->join('users', 'baskets.donor_id', '=', 'users.id');

    $this->crud->query->join('users as ass', function($join){
        $join->on('baskets.association_id', '=', 'ass.id');
        $join->orOn('baskets.association_id', 'is', \DB::raw('null');
    }

    $this->crud->addClause('where', 'users._lft', '>=', Auth::user()->_lft);
    $this->crud->addClause('where', 'users._rgt', '<=', Auth::user()->_rgt);

Of course not working. Have you got an idea ?

Thanks

Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109
David Auvray
  • 288
  • 1
  • 3
  • 20

1 Answers1

1

Use this:

$join->orWhereNull('baskets.association_id');
Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109