我正在學習Laravel,而且還有很多東西需要學習。最近在練習,現在我希望在同一頁面中根據類別的選擇查詢表格「機器人」,然後如果需要,根據該選擇創建一個新查詢並添加一個價格範圍的選擇。但是它得到一個錯誤:「array_key_exists()期望參數2是數組,給定的整數」。我甚至不知道錯誤到底在哪裏。如何調試「array_key_exists()期望參數2爲數組,整數給出」在PHP中?
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' => '[email protected]', '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' => '[email protected]', 'method' => 'GET')) !!}
{!! Form::hidden('modelesc', $robot->Model) !!}
{!! Form::submit($robot->Model) !!}
{!! Form::close() !!}
@endforeach
</div>
</main>
layout.blade(其中I放置jQuery的)
$('#categoryesc').on('change', function chcat(){
$(this).closest('form').submit();
});
$('#priceesc').on('change', function chpri(){
$(this).closest('form').submit();
});
我沒有看到你的代碼中的任何array_key_exists()。請修改我編輯代碼的代碼片段 – Nevermore