簡體   English   中英

如何在PHP中調試“ array_key_exists()期望參數2為數組,給定整數”?

[英]How to debug “array_key_exists() expects parameter 2 to be array, integer given” in PHP?

我正在學習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();

除非您使用orWhere()或像where('Price', '<=', $priceesc, 'or')這樣使用,否則它們將在后台自動與AND鏈接在一起。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM