繁体   English   中英

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

[英]array_merge(): Argument #2 is not an array on 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/”错误。 以下是controller和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'));
        });

        $.get("/genre-tags/", {id: id}, function(data)
        {
            hideLoader();
            refreshedPage = $(data);
            newDemo = refreshedPage.find(".libraryWrapper, .albumWrapper, .composersWrapper, .albumsListWrapper, .albumsWrapper, .accountWrapper, .distributionsWrapper, .PaymentsWrapper, .contactWrapper, .contractsWrapper, .toolsWrapper, .blogWrapper, .pageWrapper, .cartWrapper, .pageWrapper").html();

            $('.libraryWrapper, .albumsWrapper, .albumWrapper, .composersWrapper, .albumsListWrapper, .accountWrapper, .distributionsWrapper, .PaymentsWrapper, .contactWrapper, .contractsWrapper, .toolsWrapper, .blogWrapper, .pageWrapper, .cartWrapper, .pageWrapper').html(newDemo);
            activatePlayer()
            initTrackInfo();
        });
    });
});

Route::get('genre-tags/', ['as' => 'tag.popular-genres', 'uses' => 'SideFilterController@genresFilter']);

//checkbox blade file

<li class="lib">
            <label><input class="genreTag"  id="{{ $genreTag->id}}" type="checkbox" name="" value="{{ $genreTag->name }}">{{{ $genreTag->name }}} ({{count(json_decode($genreTag->elements, true))+ count(json_decode($genreTag->tracks, true))}}) </label>
        </li>

我认为您的 controller 中存在问题:您有 whereIn 查询需要在第二个参数上传递数组。 但是您的 ID 是 null,这就是为什么会出现这样的错误。 尝试将代码更改为以下将起作用。

public function genresFilter ()
    {
        $sortBy = Input::get('sortBy', 'id');
        $dir    = Input::get('direction', 'desc');

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

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

        // check if id is not array, then give empty array
        if(!is_array($id)) $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'));
    }

解决方案 2

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);
        
        // if id is not null and and array then we do filter
        if($id != null && is_array($id)) {
           $tag->whereIn('id', $id);
        }

        $tag->get();

        echo "<pre>";
        print_r($tag->toArray());
        die();

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM