0

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?

jarlh
  • 42,561
  • 8
  • 45
  • 63
  • What error you are receiving ? You have to use `get()` to get result – Roshni hegde Apr 06 '18 at 10:00
  • You are using WhereNotIn incorrectly, it should be `$query->whereNotIn('column_name', $someArrayHere)` – HelloSpeakman Apr 06 '18 at 10:02
  • @HelloSpeakman it's ```Material::with(['orders_material' => ->whereNotIn('Material_id', function($q) use ($begin_date, $end_date, $begin_hour, $hour_final) { $q->where('date_begin', '>=', $begin_date)```, but column_name it's a pivot name, what i should select ? –  Apr 06 '18 at 10:04

1 Answers1

1

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();
Brian Lee
  • 17,904
  • 3
  • 41
  • 52