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 t
o 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.
WhereIn expects array as a second parameter as it is structured to check if field value is within the array.
whereIn('Price', '<=', $priceesc) will not work as second parameter is string and internally its expecting array and doing foreach on it.
You need to replace it with :
where('Price', '<=', $priceesc)
First, there is the whereIn
issue addressed by the other answers.
However, in addition to that, the actual issue you're running into is that $robots
is being set to two different things depending on the result of your if
statements.
Inside if (empty($modelesc))
, $robots
is being set to a Collection
of stdClass
objects. When you foreach
this result, you will loop through the Collection
.
However, inside the elseif (Auth::check())
, $robots
is being set to an individual stdClass
instance (you have used the first()
method). When you foreach
this result, you'll be looping through the properties on that one instance, which is not what you intended. One of the properties on this instance is null
, which is what you are seeing.
A quick solution would be to wrap this result in a new collection:
$robots = collect([DB::table('robots')->where('Model', $modelesc)->first()]);