0

J'ai environ 50 objets dans un tableau.laravel 5.4 pagination problème argument non valide fourni pour foreach()

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

$users = json_decode($user,true); 

Ai-je placé ->simplePaginate(15) correctement? Puis-je faire json_decode après ça? comme je le fais? Je ne reçois aucune erreur dans le contrôleur.

Vues

@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 

Même si je ne mettre ce code {{ $users->links() }} vu que je reçois toujours la même erreur. Je suppose que le problème est avec le code de pagination sur le contrôleur.

Edité

J'ai ce petit problème encore:

Avant

<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> 

Après

<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> 

Cela donne après le code me donne l'erreur en essayant d'obtenir la propriété de non -object c'est pourquoi j'utilisais json_decode() et B Avant le code. Comment puis-je résoudre ces problèmes?

résultat

{ 
    "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 
     }, 

Modèle utilisateur

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'); 
} 

Modèle Campus

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

Année Modèle

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

Répondre

1

Vous n'avez pas besoin de coder json et d'accéder directement à la propriété de l'objet.

Contrôleur

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

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

Voir

<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

J'ai ajouté plus de code. J'ai changé le code comme ceci: $ user-> john-> doe pour $ user ['john'] ['doe'] mais alors il jette une erreur. Essayer d'obtenir la propriété de non-objet. C'est la raison pour laquelle j'ai choisi json_decode. Comment puis-je réparer ça? –

+0

@MurlidharFichadia Si vous êtes impatient de charger la relation et qu'ils existent, cela ne devrait pas arriver. Pouvez-vous lancer 'dd ($ users);' avant la vue de retour et partager les résultats – Sandeesh

+0

J'ai ajouté des résultats. juste l'un des objets du tableau. si vous voulez que j'ajoute plus d'objets, je vais le mettre à jour –