0

我在數組中有大約50個對象。laravel 5.4分頁問題爲foreach提供了無效參數()

$user = User::with('abc','def','ghi','jkl','mno','pqr') 
     ->where([['id', '<>', Auth::user()->id],['role', '<>', 2]])->simplePaginate(15); 

$users = json_decode($user,true); 

我把->simplePaginate(15)這個正確嗎?那我可以做json_decode嗎?像我在做什麼?我沒有得到控制器的任何錯誤。

在瀏覽

@section('content') 
<div class="container-fluid"> 
<div></br></br></div> 
@include('errors.list') 

<div class="row"> 
    @foreach($users as $user) 
    <a href="#" onclick=document.getElementById('{{$user->id}}').style.display='block'> 
    <div class="col-md-3" style="margin-bottom:50px;"> 
    @if($user["search_status"] == 'matched') 
     <div class="w3-card-12-disabled" style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;"> 
      @else 
     <div class="w3-card-12" style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;"> 
     @endif 
     <img align="center" src="{{$user->photo}}" style=" border-radius:50%; ; border-style: solid; border-color: #333; border-width: 5px; margin-left:15%;" width="150px !important" height="150px !important" /> 
     <h3 style="color:#000; 
    padding-top: 20px;" align="center">{{ $user->firstname}}</br>{{ $user->lastname}}</h3> 

     <div style="display:flex; justify-content: center;"> 
     <div class="tooltip2"> 
     <img align="middle" style="" src="{{$user->natives->flag}}"/> 
     <span class="tooltiptext">{{$user->natives->language}}</span> 
     </div> 
    </div> 

    </br> 
     </div> 
    </div>  
    </a>  
    @endforeach 



    <div class="row"> 
    @foreach($users as $user) 
    <div id="{{$user->id}}" class="w3-modal"> 
     <div class="w3-modal-content w3-animate-top w3-card-8"> 
      <div id="div1" style="padding:40px 30px 30px 30px;"> 
       <img style="border-radius:50%; border:5px solid #333; margin-left:18px" src="{{$user->photo}}" width="200px !important" height="200px !important"> 
       <div style="text-align: center; padding-top:70px;"> 
        {!! Form::open(['url' => '/users/'. $user->id.'/reportc']) !!} 
        {!! Form::hidden('_method', 'POST') !!} 
        {!! Form::hidden('friend', $user->id, null, ['class'=>'form-control']) !!} 
        {!! Form::submit('Report', ['class'=>'btn', 'style' => 'padding: 10px 10px 10px 10px; color:#000; border-color:#000;']) !!} 
        {!! Form::close() !!} 
       </div> 
       </div> 
      <div id="div2" style="padding:40px 0px 40px 0px;"> 
      <span onclick=document.getElementById('{{$user->id}}').style.display='none' 
        class="w3-closebtn">&times;</span> 

       <h1>{{ $user->firstname}} {{ $user->lastname}}</h1> 
       </br> 


       </br> 
       <h5 style="padding-top:30px;"><b>About Me:</b> {{ $user->bio}} </h5> 
       <h5 style="padding-top:10px;"><b>Campus:</b> {{$user->campus->campus_name}}</h5> 
       <h5 style="padding-top:10px;"><b>School:</b> {{ $user->schools->schools_name}} </h5> 
       <h5 style="padding-top:10px;"><b>Year:</b> {{ $user->years->years_name}} </h5> 
       <h5 style="padding-top:10px;"><b>Native Speaker of:</b> {{$user->natives->language}} </h5> 
       <h5 style="padding-top:10px;"><b>Second Native Speaker of:</b> {{ $user->snatives->language }} </h5> 

       <h5 style="padding-top:10px;"><b>Seeking Speaker of:</b> 
       @foreach($seeks as $seek) 
       @if($seek->user_id === $user->id) 
        {{ $seek->language}}, 
       @endif 
       @endforeach </h5> 


      </div> 
     </div> 
     </div> 
    @endforeach 
    </div> 

    {!! $users->links() !!} 

</div> 
</div> 
@endsection 

即使我不把這個代碼{{ $users->links() }}鑑於我仍然得到同樣的錯誤。我想問題是與控制器上的分頁代碼。

編輯

我有這個小問題還是:

之前

<h5 style="padding-top:10px;"><b>Campus:</b> {{$user['campus']['campus_name']}}</h5> 
       <h5 style="padding-top:10px;"><b>School:</b> {{ $user['schools']['schools_name']}} </h5> 
       <h5 style="padding-top:10px;"><b>Year:</b> {{ $user['years']['years_name']}} </h5> 
       <h5 style="padding-top:10px;"><b>Native Speaker of:</b> {{$user['natives']['language']}} </h5> 
       <h5 style="padding-top:10px;"><b>Second Native Speaker of:</b> {{$user['snatives']['language']}} </h5> 

       <h5 style="padding-top:10px;"><b>Seeking Speaker of:</b> 
       @foreach($seeks as $seek) 
       @if($seek->user_id === $user['id']) 
        {{ $seek->language}}, 
       @endif 
       @endforeach </h5> 

<h5 style="padding-top:10px;"><b>Campus:</b> {{$user->campus->campus_name}}</h5> 
       <h5 style="padding-top:10px;"><b>School:</b> {{ $user->schools->schools_name}} </h5> 
       <h5 style="padding-top:10px;"><b>Year:</b> {{ $user->years->years_name}} </h5> 
       <h5 style="padding-top:10px;"><b>Native Speaker of:</b> {{$user->natives->language}} </h5> 
       <h5 style="padding-top:10px;"><b>Second Native Speaker of:</b> {{$user->snatives->language}} </h5> 

       <h5 style="padding-top:10px;"><b>Seeking Speaker of:</b> 
       @foreach($seeks as $seek) 
       @if($seek->user_id === $user->id) 
        {{ $seek->language}}, 
       @endif 
       @endforeach </h5> 

這使得代碼給出了後我錯誤試圖獲得的非財產-object,這就是我使用json_decode()和B的原因在代碼之前。我如何解決這些問題?

結果

{ 
    "per_page":12, 
    "current_page":1, 
    "next_page_url":"http:\/\/localhost:8000\/users\/community?page=2", 
    "prev_page_url":null, 
    "from":1, 
    "to":12, 
    "data":[ 
     { 
     "id":5, 
     "firstname":"Beth", 
     "lastname":"Hanley", 
     "email":"[email protected]", 
     "role":1, 
     "photo":"uploads\/2.jpg", 
     "bio":"Hockey fan, foodie, gamer, Saul Bass fan and HTML5 Guru.", 
     "campus":{ 
      "id":1, 
      "campus_name":"name" 
     }, 
     "school":"1", 
     "year":"1", 
     "native_lang":"2", 
     "native_lang_flag":"2", 
     "search_status":"available", 
     "created_at":null, 
     "updated_at":null, 
     "lastlogin":null, 
     "verification_code":null, 
     "isverify":"0", 
     "native_lang_2":null, 
     "native_lang_flag_2":null, 
     "schools":{ 
      "id":1, 
      "schools_name":"School of Energy, Geoscience, Infrastructure and Society" 
     }, 
     "years":{ 
      "id":1, 
      "years_name":"First" 
     }, 
     "flags":{ 
      "id":2, 
      "name":"\u00c5land Islands", 
      "flag":"\/flags\/AX-32.png" 
     }, 
     "natives":{ 
      "id":2, 
      "language":"English", 
      "flag":"\/flags\/GB-32.png" 
     }, 
     "snatives":null 
     }, 

用戶模型

public function friends() 
{ 
    return $this->hasMany('App\Friend','friend_id','id'); 
} 

public function seeks() 
{ 
    return $this->belongsToMany('App\Language'); 
} 

public function natives() 
{ 
    return $this->hasOne('App\Language','id','native_lang'); 
} 

public function snatives() 
{ 
    return $this->hasOne('App\Language','id','native_lang_2'); 
} 

public function roles() 
{ 
    return $this->hasOne('App\Role','id','role'); 
} 

public function campus() 
{ 
    return $this->hasOne('App\Campus','id','campus'); 
} 

public function schools() 
{ 
    return $this->hasOne('App\School','id','school'); 
} 

public function years() 
{ 
    return $this->hasOne('App\Year','id','year'); 
} 

public function flags() 
{ 
    return $this->hasOne('App\Flag','id','native_lang_flag'); 
} 

園區模式

public function users() 
{ 
    return $this->belongsTo('App\User','campus','id'); 
} 

年份型號

public function users() 
{ 
    return $this->belongsTo('App\User','year','id'); 
} 

回答

1

你不需要json編碼任何東西並直接訪問對象屬性。

控制器

$users = User::with('abc','def','ghi','jkl','mno','pqr') 
    ->where([ 
     ['id', '<>', auth()->id()], 
     ['role', '<>', 2] 
    ])->simplePaginate(15); 

return view('someview', compact('users')); 

查看

<div class="row"> 
    @foreach($users as $user) 
     <a href="#" onclick="document.getElementById('{{ $user->id }}').style.display='block'"> 
      <div class="col-md-3" style="margin-bottom:50px;"> 
       @if($user->search_status == 'matched') 
        <div class="w3-card-12-disabled" 
         style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;"> 
         @else 
          <div class="w3-card-12" 
           style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;"> 
           @endif 
           <img align="center" src="{{ $user->photo }}" 
            style=" border-radius:50%; ; border-style: solid; border-color: #333; border-width: 5px; margin-left:15%;" 
            width="150px !important" height="150px !important"/> 
           <h3 style="color:#000; 
    padding-top: 20px;" align="center">{{ $user->firstname }}<br/>{{ $user->lastname}}</h3> 

           <br/> 
          </div> 
        </div> 
      </div> 
     </a> 
    @endforeach 
</div> 

{!! $users->links() !!} 
+0

我加入了更多的代碼。我改變了這樣的代碼:$ user-> john-> doe for $ user ['john'] ['doe']但是它會拋出錯誤。試圖獲得非對象的屬性。這是我去json_decode的原因。我該如何解決? –

+0

@MurlidharFichadia如果你急於加載關係並且存在,這不應該發生。你可以運行'dd($ users);'在返回視圖前分享結果 – Sandeesh

+0

我已經添加了結果。只是其中一個數組對象。如果你想讓我添加更多的對象,我會更新它 –

相關問題