laravel - null value in foreach

advertisements

i would like to know how to solve this problem. Im making a query with a "whereIn" clause (In this case, its named "Price") in which there is a "<=" operator. But im getting an error which indicates that one variable is null. Im using laravel 5.4.

code used so far: controller:

function catalog(Request $abc) {
    $categoryesc = $abc->categoryesc;
    $manufaesc = $abc->manufaesc;
    $priceesc = $abc->priceesc;
    $modelesc = $abc->modelesc;
    if (empty($modelesc)) {

        $robots = DB::table('robots')->whereIn('Category_id', [$categoryesc])->whereIn('Manufacturer_id', [$manufaesc])->whereIn('Price', '<=', $priceesc)->get();
    }elseif (Auth::check()){
        $robots = DB::table('robots')->where('Model', $modelesc)->first();
        $colours = DB::table('colours')->pluck('Colour', 'id');
    }
    else {
        return redirect('login');
    }
    return view('catalog', compact('robots', 'categories', 'colours'));
}

blade file:

@if (empty($_GET['modelesc']))  

<h1>Catalog</h1>

{{ Form::open(array('method' => 'GET')) }}
    {{ Form::select('categoryesc', ['3,2,1' => 'Any Category', '1' => 'Light', '2' => 'Medium', '3' => 'Heavy'], '3,2,1') }}
    {{ Form::select('manufaesc', ['2,1' => 'Any Make', '1' => 'Xenon', '2' => 'Tauron'], '2,1') }}
    {{ Form::select('priceesc', ['1000000' => 'Any Price', '100' => '100', '200' => '200'], '1000000') }}
    {{ Form::submit('Search') }}
{{ Form::close() }}

<div>Available models</div>
<b>On this page ({{ $robots->count() }} robots)</b>

<div id="area1">
@foreach($robots as $robot)
  {{ Form::open(array('method' => 'GET')) }}
  {{ Form::hidden('modelesc', $robot->Model) }}
  {{ Form::submit($robot->Model) }}
  {{ Form::close() }}
@endforeach
</div>

@else

<h1>Orders</h1>

<a href="/catalog"><button> < </button></a>

{{ Form::open(['method' => 'POST']) }}
  {{ Form::hidden('users_id', Auth::user()->id) }}
  Model: {{ Form::text('Model', $robots->Model) }}<br><br>
  {{ Form::hidden('Category_id', $robots->Category_id) }}
  {{ Form::hidden('Fabrication_date', date('Y-m-d')) }}
  Choose colour: {{ Form::select('Colour_id', $colours) }}<br><br>
  {{ Form::hidden('Order_status_id', '1') }}
  {{ Form::submit('Order') }}
{{ Form::close() }}
@endif

In the builder.php i previously added the "<=" operator to the function "invalidOperatorAndValue" at page 615 (in order for it to work). This is the error message:

ErrorException in Builder.php line 766:
Invalid argument supplied for foreach()
in Builder.php line 766
at HandleExceptions->handleError(2, 'Invalid argument supplied for
foreach()',
'C:\\Users\\Joao_Carvalho\\Laravel\\MegaRobot\\
vendor\\laravel\\framework\\src\\Illuminate\\Database\\Query\\Builder.php',
766, array('column' => 'Price', 'values' => '<=', 'boolean' => null, 'not'
=> false, 'type' => 'In')) in Builder.php line 766
at Builder->whereIn('Price', '<=', null) in CentralController.php line 26


Your whereIn for the condition on Price should be a where instead. whereIn is not the appropriate method for when you want to do a comparison with <=:

DB::table('robots')
    ->whereIn('Category_id', [$categoryesc])
    ->whereIn('Manufacturer_id', [$manufaesc])
    ->where('Price', '<=', $priceesc)
    ->get();

whereIn on the other hand should be used for testing whether a value is in an array, which is what you do in the two other cases.