laravel 5.4 pagination issue invalid argument supplied for foreach()

558 views Asked by At

I have around 50 objects in an array.

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

$users = json_decode($user,true);

Have I placed ->simplePaginate(15) this correctly? Can I do json_decode after that? like I am doing? I dont get any error in controller.

In Views

    @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

Even if I dont put this code {{ $users->links() }} in view I still get same error. I guess issue is with the pagination code on controller.

Edited

I have this small issue still:

Before

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

After

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

This gives After code gives me error trying to get property of non-object which is why I was using json_decode() and Before code. How do I fix these?

result

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

User Model

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

Campus Model

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

Year Model

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

There are 1 answers

9
Sandeesh On BEST ANSWER

You don't need to json encode anything and access the object property directly.

Controller

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

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

View

<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() !!}