【问题标题】:How to debug "array_key_exists() expects parameter 2 to be array, integer given" in PHP?如何在 PHP 中调试“array_key_exists() 期望参数 2 为数组,给定整数”?
【发布时间】:2017-03-14 17:51:43
【问题描述】:

我正在学习 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'));
}

目录.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();
        });

【问题讨论】:

  • 我在您的代码中没有看到任何 array_key_exists()。请修改代码-sn-p

标签: php jquery mysql laravel


【解决方案1】:

这个查询是导致错误的原因:

$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', '&lt;=', $priceesc, 'or') 一样使用它,否则它们将在幕后自动与 AND 链接在一起

【讨论】:

  • 我编辑了代码。现在它给出错误:“非法运算符和值组合”
  • 看起来您有 2 个表单,这意味着当您提交一个表单时,您不会从另一个表单中提取输入,这取决于 $request-&gt;categoryesc;$priceesc = $request-&gt;priceesc; 中的哪一个为空,而您是无法使用where('Price', '&lt;=', null),这很可能是导致您的错误的原因。
猜你喜欢
  • 2020-10-28
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 2017-11-14
  • 2016-03-23
  • 1970-01-01
  • 2019-12-15
  • 2020-01-27
相关资源
最近更新 更多