期望参数2为数组

期望参数2为数组

我正在学习Laravel,而且还有很多东西要学习。最近一直在练习,现在出现一种情况,我想在同一页面上根据类别的选择对表“ robots”进行查询,然后如果需要,根据该选择进行新查询并添加一个价格范围的选择。但它得到一个错误:“ 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();


它们会在后台自动与AND链接在一起,除非您使用orWhere()或像where('Price', '<=', $priceesc, 'or')那样使用它

关于php - 如何在PHP中调试“array_key_exists()期望参数2为数组,给定整数”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42793176/

10-12 12:22