2014-05-21 4 views
2

내 사용자 페이지에서 두 번째 탭에 '제품'과 '팔로어'탭이있는 div를 사용합니다. 추종자 목록에 대한 페이지 매김 페이지 매김을 시도 할 때 "제품"탭으로 돌아 가지 않습니다. 사용자는 Ajax를 호출합니다. 하지만 그것을 실행하려고하면, 나는 오류 (500)가 있고, 가져 오기의 응답은 다음과 같습니다ErrorException, array_merge() : 호출 할 때 인수 # 2가 배열이 아닙니다. Ajax

{"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('[email protected]', '<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('[email protected]', '<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('[email protected]', $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('[email protected]', $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('[email protected]', '<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('[email protected]', '<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 
+1

'array_merge()'를 사용하는 방법을 제외한 모든 것을 보여 줬습니다. –

+0

저는 array_merge를 사용하지 않았습니다. 모든 코드가 작동합니다. 컨트롤러는 뷰에 정보를 보내고 페이지 매김을 사용할 때 js를 호출하고 방화 광구와 함께 응답을 볼 때 ErrorException : Array_merge를 볼 수 있습니다. .. – Malxiin

+0

$ user의 정보를 회복했기 때문일 수 있습니까? – Malxiin

답변

3

laravel ErrorException, array_merge(): Argument #2 is not an array에 대한 Google의 첫 번째 검색 결과이기 때문에이 질문에 마주 쳤습니다.

질문이 1 년이 지난 지금도 관련성이 있습니다. 이는 Laravel 5.0에서 나에게 일어났기 때문입니다.

모든 코드를 소화 할 시간을 들이지 않아도 문제는 내 경우와 같을 수 있습니다. 두 번째 인수로 배열을 두 번째 인수로 view::make() (의도적이든 아니든간에) 전달할 때 배열을 전달해야합니다.

문제 라인은 컨트롤러,이 하나입니다

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

$json 개체로 나타납니다 점을 감안, 수정은 간단하다 :

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

바라건대,이 대답은 미래에 다른 사람을 도울 것입니다.

+0

Laravel 5.1을 사용하고 있는데 뷰의 매개 변수로 배열 대신 객체를 전달하려고합니다. 내 컨트롤러에는 다음과 같은 것들이 있습니다 :'return view ('path_to_view', $ array_of_parameters); – DavidHyogo