【问题标题】:ErrorException, array_merge(): Argument #2 is not an array when i work a call AjaxErrorException,array_merge():当我调用 Ajax 时,参数 #2 不是数组
【发布时间】:2014-05-21 14:16:41
【问题描述】:

在我的用户页面中,我使用带有两个选项卡“产品”和“关注者”的 div,在第二个选项卡中,我为关注者列表设置了分页。 尝试分页时不返回“产品”选项卡,我使用了一个调用 Ajax。 但是当我尝试运行的时候,报错500,get的响应是:

{"error":{"type":"ErrorException","message":"array_merge(): Argument #2 is not an
array","file":"\/Users\/utilisateur\/Desktop\/mamp\/ptf-l4\/vendor\/laravel\/framework\
/src\/Illuminate\/View\/Environment.php","line":117}} 

我不知道为什么,也不知道如何找到解决方案。

这是我的控制器:

public function show($id){

    // récupère les données de l'user et les produis qu'il partage
    $show = $this->api->show($id);

    // décode le json pour qu'il se réaffiche en array pour pouvoir l'exploiter
    $json = json_decode($show->getContent());

    $json->followed = User::userFollowedPaginate($id);

    LaravelMixpanel::identify(Auth::user()->mixpanel_id);
    LaravelMixpanel::track('User show view');

    if(Request::ajax())
    {
        $html = View::make('users.show', $json)->render();

        return Response::json(['html' => $html]);
    }

    return View::make('users.show')
                ->with('user', $json);
}

我的 API/控制器:

public function show($id){

    // récupère donnée de l'user et les produits qu'il échange
    $data = User::with(array('products' => function($query){
                                                $query->whereNull('shared_at');
                                                $query->whereNull('deleted_at');
                                            }))->findOrFail($id)->toArray();

    // récupère le nb de produits qu'il échange en ce moment
    $data['nbShareCurrently'] = Product::where('user_id', $id)->whereNull('shared_at')->whereNull('deleted_at')->count();

    // récupère le nb de produits qu'il a échangé depuis le début
    $data['nbShared'] = Product::where('user_id', $id)->whereNotNull('shared_at')->count();

    return Response::json($data, 200);
}

我的 JS:

function callAjaxUser(url) {

    $.ajax ({
        type: "GET",
        url: url ,
        success: function() {
            console.log('Success ');   
        },
        error: function() {
            console.log('Error '); 
        }
    });
}



$(document).ready(function(){
    $('body').on('click', ' .pagination a ', function(event){
        event.preventDefault();
        var url=$(this).attr('href');
        callAjaxUser(url);
    });
});

我的看法:

@extends('default')
@section('title')
    Fiche de {{ $user->name }}
@stop
@section('contenu')
    <section class="panel col-lg-8 col-sm-8 m-t-large">
        <header class="panel-heading m-l-n m-r-n">
            <ul class="nav nav-tabs pull-right">
                <li class="active"><a href="#product" data-toggle="tab"><i class="icon-list icon-large text-default"></i>Mes produits en cours de partage</a></li>
                <li><a href="#follow" data-toggle="tab"><i class="icon-group icon-large text-default"></i>Mes abonnés</a></li>
            </ul>
            <span class="hidden-sm">Fiche technique du frigo</span>
        </header>
        <div class="panel-body">
            <div class="tab-content">              
                <div class="tab-pane active" id="product">
                    <div class="col-lg-3">
                        {{-- dd($user) --}}
                        <h4>{{{ $user->name }}}</h4>
                        @if($user->photo && File::exists(public_path().'/uploads/photos/users/'.e($user->photo)))  
                            {{ HTML::image('uploads/photos/users/'.e($user->photo), e($user->name)) }}
                        @else
                             {{ HTML::image('template/images/avatar.jpg', e($user->name)) }}
                        @endif
                        @if($user->id!=Auth::user()->id)
                             @if(DB::table('user_followers')->where('user_followed', $user->id)->where('user_following', Auth::user()->id)->count()==0)
                                {{ HTML::decode(HTML::linkAction('UserFollowersController@follow', '<i class="icon-heart">S\'abonner</i>', array('id' => $user->id), array('class' => 'btn btn-info m-t-large m-b-small'))) }}
                            @else
                                {{ HTML::decode(HTML::linkAction('UserFollowersController@unfollow', '<i class="icon-heart">Se désabonner</i>', array('id' => $user->id), array('class' => 'btn btn-danger m-t-large m-b-small '))) }}
                            @endif
                        @endif
                    </div>
                    <div class="col-lg-9">
                        <h4>Le frigo contient en ce moment :</h4>
                            <ul class="list-unstyled">
                                @foreach($user->products as $product)
                                <li class="list-group-item bg m-b-small">
                                    <div class="media">
                                        @if(e($product->photo) && File::exists(public_path().'/uploads/photos/products/'.e($product->photo)))
                                            <span class="pull-left img-product"> 
                                                <a href="{{ URL::action('ProductsController@show', $product->id) }}">
                                                    {{ HTML::image('uploads/photos/products/'.e($product->photo), e($product->title), array('class' => 'img-rounded')) }}
                                                 </a>
                                            </span>
                                        @endif
                                        <div class="media-body">
                                            <div><a href="{{ URL::action('ProductsController@show', $product->id) }}" class="h4">{{{ $product->title }}}</a></div>
                                            <p>{{{ $product->description }}}</p>
                                            <p class="pull-left">Quantité :  <span class="label label-info">{{{ $product->quantity }}}</span></p>
                                            <p class="pull-right">Prix : <span class="badge bg-danger">{{{ $product->price }}} €</span></p>
                                        </div>
                                    </div>
                                </li>
                            @endforeach
                        </ul>
                        <div class="pagination">
                            {{-- $users->products->links() --}}
                        </div>
                    </div>
                </div>
                <div class="tab-pane" id="follow">
                    <div class="row m-b-large">
                        @if($user->followed!=NULL)
                            @foreach($user->followed as $user_followed)
                                <div class="panel col-lg-2 m-l-large m-t-large ">
                                    <div class="col-lg-8 m-b-n-small col-md-offset-2 m-t-large text-center">
                                        @if($user_followed->photo && File::exists(public_path().'/uploads/photos/users/'.e($user_followed->photo)))
                                            {{ HTML::image('uploads/photos/users/'.e($user_followed->photo), e($user_followed->name), array ( 'class' => 'img-circle')) }}
                                        @else
                                            {{ HTML::image('template/images/avatar.jpg', e($user_followed->name), array ( 'class' => 'img-circle')) }}
                                        @endif
                                        <h3>{{{ $user_followed->name }}}</h3>
                                    </div>
                                    <div class="col-lg-10 m-b-small center ">
                                        @if(DB::table('user_followers')->where('user_followed', $user_followed->id)->where('user_following', Auth::user()->id)->count()==0)
                                            {{ HTML::decode(HTML::linkAction('UserFollowersController@follow', '<i class="icon-heart">S\'abonner</i>', array('id' => $user_followed->id), array('class' => 'btn btn-info btn-group-justified m-t-large m-b-small'))) }}
                                        @else
                                            {{ HTML::decode(HTML::linkAction('UserFollowersController@unfollow', '<i class="icon-heart">Se désabonner</i>', array('id' => $user_followed->id), array('class' => 'btn btn-danger btn-group-justified m-t-large m-b-small '))) }}
                                        @endif
                                    </div>
                                </div>
                            @endforeach
                        @else
                            <div class="panel col-lg-8 col-lg-offset-2 m-t-large">
                                <h4> Tu ne suis actuellement personne </h4>
                            </div>
                        @endif
                    </div>    
                    <div class="col-lg-12">
                        {{ $user->followed->links() }}
                    </div>
                </div>
            </div>
        </div>
    </section>
@stop
@section('js')
    {{ Basset::show('usersPaginate.js') }}
@endsection

【问题讨论】:

  • 除了array_merge()的使用方式之外,您已经展示了所有内容。
  • 我没有使用array_merge,它是所有工作的代码,控制器将信息发送到视图,当我使用分页时,我调用js,当我用firebug查看响应时,我可以看到 ErrorException:Array_merge...
  • 会不会是因为$user的信息回收了?

标签: php ajax twitter-bootstrap laravel array-merge


【解决方案1】:

我偶然发现了这个问题,因为它是laravel ErrorException, array_merge(): Argument #2 is not an array 的第一个 Google 搜索结果。

即使这个问题已经存在一年了,它仍然是相关的,因为这发生在我今天的 Laravel 5.0 中。

如果不花时间消化所有代码,您的问题可能与我的问题相同:您将非数组作为第二个参数传递给 view::make()(无论是否有意)当你必须传递一个数组时。

问题行是这一行,在您的控制器中:

$html = View::make('users.show', $json)-&gt;render();

鉴于$json 似乎是一个对象,修复很简单:

$html = View::make('users.show', compact('json'))-&gt;render();

希望这个答案能在未来对其他人有所帮助。

【讨论】:

  • 我仍在使用 Laravel 5.1,我愚蠢地试图传递一个对象而不是数组作为视图的参数。在我的控制器中,我现在拥有:return view('path_to_view', $array_of_parameters);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
相关资源
最近更新 更多