猿问

array_merge(): 参数 #2 不是 sidefilter 上的数组

我在一个问题上遇到了麻烦。在侧边栏过滤器功能中,当我单击复选框时,它将正确显示列表。但是当我取消选中复选框时,它将显示“array_merge(): Argument #2 is not an array”检查元素时显示此错误。当我点击复选框时,它会生成这个 url "http://abc.local/genre-tags/?id%5B%5D=31" 。当我取消选中复选框时,它会显示错误的 url“http://abc.local/genre-tags/”。以下是控制器和js代码。


public function genresFilter ()

    {

        $sortBy = Input::get('sortBy', 'id');

        $dir    = Input::get('direction', 'desc');


        $orderBy = [

                        'tracks'=>[   'order_by'=>$sortBy, 'direction'=>$dir ]

                    ];


        $id = Input::get('id');


        $category = Category::where('slug','music-genre')->first();


        $tag = Tag::with('tracks','elements')->where('category_id', $category->id)->whereIn('id', $id)->get();

        echo "<pre>";

        print_r($tag->toArray());

        die();

        

        $this->layout->content = View::make('public.tags.genres', compact('tag'));

    }



//Sidebar Filter Genre Tracks

$(document).ready(function () {

    $('.genreTag').on('change', function (e)

    {

        

        $('input.filter-playlist, .popularGenreTag, .mood-emotion, .production-type, .vocals, .all-tracks, .last-year, .last-month, .last-week, .last-day').each(function() {

            var $this = $(this);

            $this.prop('checked', false);

            $this.parent().find('> div').removeClass('chk-checked').addClass('chk-unchecked');

        });


        e.preventDefault();

        id = [];


        $('.genreTag:checked').each(function()

        {

            id.push($(this).attr('id'));

        });

烙印99
浏览 112回答 1
1回答

冉冉说

我认为你的控制器有问题:你有 whereIn 查询需要在第二个参数上传递数组。但是你的 id 是空的,为什么它会给出这样的错误。尝试将代码更改为以下将起作用。public function genresFilter ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $sortBy = Input::get('sortBy', 'id');&nbsp; &nbsp; &nbsp; &nbsp; $dir&nbsp; &nbsp; = Input::get('direction', 'desc');&nbsp; &nbsp; &nbsp; &nbsp; $orderBy = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'tracks'=>[&nbsp; &nbsp;'order_by'=>$sortBy, 'direction'=>$dir ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; $id = Input::get('id');&nbsp; &nbsp; &nbsp; &nbsp; // check if id is not array, then give empty array&nbsp; &nbsp; &nbsp; &nbsp; if(!is_array($id)) $id = [];&nbsp; &nbsp; &nbsp; &nbsp; $category = Category::where('slug','music-genre')->first();&nbsp; &nbsp; &nbsp; &nbsp; $tag = Tag::with('tracks','elements')->where('category_id', $category->id)->whereIn('id', $id)->get();&nbsp; &nbsp; &nbsp; &nbsp; echo "<pre>";&nbsp; &nbsp; &nbsp; &nbsp; print_r($tag->toArray());&nbsp; &nbsp; &nbsp; &nbsp; die();&nbsp; &nbsp; &nbsp; &nbsp; $this->layout->content = View::make('public.tags.genres', compact('tag'));&nbsp; &nbsp; }解决方案 2public function genresFilter ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $sortBy = Input::get('sortBy', 'id');&nbsp; &nbsp; &nbsp; &nbsp; $dir&nbsp; &nbsp; = Input::get('direction', 'desc');&nbsp; &nbsp; &nbsp; &nbsp; $orderBy = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'tracks'=>[&nbsp; &nbsp;'order_by'=>$sortBy, 'direction'=>$dir ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; $id = Input::get('id');&nbsp; &nbsp; &nbsp; &nbsp; $category = Category::where('slug','music-genre')->first();&nbsp; &nbsp; &nbsp; &nbsp; $tag = Tag::with('tracks','elements')->where('category_id', $category->id);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // if id is not null and and array then we do filter&nbsp; &nbsp; &nbsp; &nbsp; if($id != null && is_array($id)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$tag->whereIn('id', $id);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $tag->get();&nbsp; &nbsp; &nbsp; &nbsp; echo "<pre>";&nbsp; &nbsp; &nbsp; &nbsp; print_r($tag->toArray());&nbsp; &nbsp; &nbsp; &nbsp; die();&nbsp; &nbsp; &nbsp; &nbsp; $this->layout->content = View::make('public.tags.genres', compact('tag'));&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答