如何在PHP中调试"array_key_exists() expects parameter 2 to be array, integer given"?



我正在学习laravel,但仍有很多东西要学习。最近一直在练习,现在我想在同一页面中根据选择类别的选择对"机器人"进行查询,然后如果我愿意,请根据该选择进行新的查询并添加一个新查询价格范围选择。但是它会遇到一个错误:" array_key_exists()期望参数2是数组,整数给定的。"我什至不知道错误在哪里。

Laravel版本为5.4。在这种情况下,表是:机器人,类别

这是代码:

控制器

function catalog(Request $request) {
    $categories = DB::table('categories')->pluck('Category', 'id');
    $categoryesc = $request->categoryesc;
    $robots = DB::table('robots')->where('Category_id', $categoryesc)->get();
    $priceesc = $request->priceesc;
    $robots2 = DB::table('robots')->where('Category_id', $categoryesc AND 'Price', '<=', $priceesc)->get();
    return view('catalog', compact('robots', 'categories'));
}

catalog.php

@extends('layouts.layout')
@include('header')
<main>
<h1>Catalog</h1>
<div>Choose the category</div>
{!! Form::open(array('method' => 'GET')) !!}
    {!! Form::select('categoryesc', ['Category', $categories],  array('onchange' => 'chcat()')) !!}
{!! Form::close() !!}
<div>Choose the price</div>
{!! Form::open(array('method' => 'GET')) !!}
    {!! Form::selectRange('priceesc', 100, 200, 300, 400, 500,  array('onchange' => 'chpri()')) !!}
{!! Form::close() !!}
<div>Choose the model</div>
<b>On this page ({{ $robots->count() }} robots)</b>
<div id="area1">
@foreach($robots as $robot)
{!! Form::open(array('action' => 'RobotController@orders', 'method' =>  'GET')) !!}
{!! Form::hidden('modelesc', $robot->Model) !!}
{!! Form::submit($robot->Model) !!}
{!! Form::close() !!}
@endforeach
</div>
<div id="area2">
@foreach($robots2 as $robot2)
{!! Form::open(array('action' => 'RobotController@orders', 'method' =>  'GET')) !!}
{!! Form::hidden('modelesc', $robot->Model) !!}
{!! Form::submit($robot->Model) !!}
{!! Form::close() !!}
@endforeach
</div>
</main>

layout.blade(我放置jQuery)

        $('#categoryesc').on('change', function chcat(){
        $(this).closest('form').submit();
        });
        $('#priceesc').on('change', function chpri(){
        $(this).closest('form').submit();
        });

此查询是导致错误的原因:

$robots2 = DB::table('robots')->where('Category_id', $categoryesc AND 'Price', '<=', $priceesc)->get();

您需要制作每个where它自己的方法,例如:

$robots2 = DB::table('robots')->where('Category_id', $categoryesc)->where('Price', '<=', $priceesc)->get();

除非您使用orWhere()或使用where('Price', '<=', $priceesc, 'or')

,否则它们将与AND一起自动链接在一起。

相关内容

最新更新