我正在学习 Laravel,还有很多东西需要学习。最近一直在练习,现在我想在同一页面中根据类别的选择对表“机器人”进行查询,然后如果我愿意,可以根据该选择进行新的查询并添加价格范围选择。但它得到一个错误:“array_key_exists() 期望参数 2 是数组,给定整数”。我什至不知道错误到底在哪里。
Laravel 版本是 5.4。本例中的表是:机器人、类别
这是代码:
Controller
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();
});
最佳答案
此查询是导致错误的原因:
$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() expects parameter 2 to be array, integer given"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42793176/