mercredi 20 février 2019

Combining inner join and where statements in Eloquent

I am trying to convert the following database query to use Eloquent:

$orderitems = DB::table('order_items')
    ->join('orders', 'orders.id', '=', 'order_items.order_id')
    ->where('orders.status', 1)
    ->where('order_items.sku', $sku)
    ->select('order_items.*')
    ->get();

I've tried the following:

$orderitems = Item::where('sku', $sku)
    ->with(['order' => function ($query) {
        $query->where('status', 0);
    }])
    ->get();

but it seems to be bringing back more results than it should. I can see from using DB::getQueryLog that the order_items query is not joining with the orders table:

select * from `order_items` where `stock_code` = ? and `order_items`.`deleted_at` is null

Do I need to create a new relationship between orderitems and order and add the additional where into that?

Relationships:

  • Order hasMany Items
  • Item hasOne Order


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire