【发布时间】: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