lipa's avatar
Level 1

Pagination doesn't work

I'm trying to create simple pagination but I get error:

Undefined property: Illuminate\Pagination\Paginator::$name (View: /home/lipa/php/awsomelink/app/views/user/index.blade.php) 

My controller:

    $user = User::with('urls')->find($id)->paginate(10);

My View:

@foreach($user->urls as $link)
<li>
{{ HTML::link($link->shortened, "awsomelink.com/$link->shortened") }} 
{{ Form::open(array('route' => array('url.delete', $link->id), 'method' => 'delete', 'class' => 'destroy')) }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
<li>
@endforeach
<p>{{ $user->links(); }}</p>

0 likes
6 replies
mstnorris's avatar

Can you dd($user) before you call paginate on it, to make sure you actually have a User.

Try this:

$user = User::with('urls')->whereId($id)->paginate(10);
mstnorris's avatar
Level 55

From what I understand they want to paginate the User's URLS.

$user = User::with('urls')->whereId($id)->firstOrFail(); // or just $user = User::findOrFail($id);
$urls = $user->urls()->paginate(10);

// then pass both variables to the view
return view('user.index', compact('user', 'urls'));

// and loop through the products directly
@foreach ($urls as $url) ...

See this post here https://laracasts.com/discuss/channels/general-discussion/eager-loading-with-pagination/replies/31223

lipa's avatar
Level 1

@mstnorris

Part of my dd:

object(User)#209 (20) { ["table":protected]=> string(5) "users" ["hidden":protected]=> array(2) { [0]=> string(8) "password" [1]=> string(14) "remember_token" } ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(7) { ["id"]=> string(1) "3" ["email"]=> string(30) "Quinten.Bartoletti@hotmail.com" ["password"]=> string(60) 

@bestmomo I want to paginate user's links. I show all links from selected user in my View and now I want to paginate this links. View code in 1 post.

Please or to participate in this conversation.