简体   繁体   中英

How to use WhereNotIn and with in Laravel?

This is my Query eloquent created, i need to get materials that are available in dates.

I have guided from here: Room Booking Query

How to fix these errors?

Material::with(['orders_material' => function($query)  use ($begin_date,
    $end_date, $begin_hour, $hour_final) 

 {
    $query->whereNotIn(
    $query->where('date_begin', '>=', $begin_date)
    ->where('date_begin', '<=', $end_date)
    ->where('date_final', '>=', $begin_date)
    ->where('date_final', '<=', $end_date)
    ->where('hour_begin', '>=', $begin_hour)
    ->where('hour_begin', '<=', $hour_final)
    ->where('hour_final', '>=', $begin_hour)
    ->where('hour_final', '<=', $hour_final);
    //->wherePivot('Material_id', null)
    //->wherePivot('Orders_id', null)
    );
  }

The syntax is not correct, what syntax I can use?

whereHas is the appropriate clause in this case.

Also, use whereBetween when querying for values falling between a min and max. It will simplify your query quite a bit:

Material::whereHas(['orders_material' => function($query) use ($begin_date, $end_date, $begin_hour, $hour_final) {
    $query->whereBetween('date_begin', [$begin_date, $end_date])
          ->whereBetween('date_final', [$begin_date, $end_date])
          ->whereBetween('hour_begin', [$begin_hour, $hour_final])
          ->whereBetween('hour_final', [$begin_hour, $hour_final]);
})->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM