Why is the pagination not working when I use this query is that any differences in both the queries?
$users = User::all()->paginate(2);
Pagination applies only when I use this query but problem occur in index.blade it cannot fetch photo, created_at, updated_at from user I remove these three name from index.blade then it works fine.
$users = DB::table('users')->paginate(2);
When I used second query than it cannot fetch date and error show Call to a member function diffForHumans() on a non-object
@if($users)
@foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td><img height="50" src=" {{isset($user->photo) ? $user->photo->file : 'http://placehold.it/200X200'}}" ></td>
<td><a href="{{route('admin.users.edit', $user->id)}}"> {{$user->name}}</a></td>
<td>{{$user->email}}</td>
<td>{{$user->is_active == 1 ? 'Active' : 'Not active'}}</td>
<td>{{$user->created_at->diffForHumans()}}</td>
<td>{{$user->updated_at->diffForHumans()}}</td>
@if(!empty($user->role->name))
<td>{{$user->role->name}}
@endif
</td>
<td><a href="{{route('admin.users.edit', $user->id)}}">Edit</a></td>
</tr>
@endforeach
@endif
Your query should be as:
$users = User::paginate(2);
And User::all()
returns a collection, so you cannot run paginate()
on that.