dougui5419 2017-05-07 18:15
浏览 85
已采纳

laravel - foreach中的null值

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
  • 写回答

3条回答 默认 最新

  • dprfe04886 2017-05-07 18:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿