blitzkin's avatar

pagination help

two tables: users attendances

user model = hasMany('App/Attendance'); attendance model = belongsTo('App/User');

the problem is it displays error when using this in controller:

    $user_id = auth()->user()->id;
    $user = User::find($user_id)->attendance()->paginate(10);

    return view('/dashboard')->with('attendances', $user->attendances);

dashboard view has common tags and @foreach for each record and tried using:

{{ $attendances->links() }} or {{ $attendances->render() }}

always get's this error:

"Call to undefined method Illuminate\Database\Query\Builder::attendance()"

please help thank you

0 likes
16 replies
rin4ik's avatar

try now:)

 $user = User::where('id', $user_id)->attendance()->paginate(10);
1 like
blitzkin's avatar

hi tnx for the reply..

still the same error:

"Call to undefined method Illuminate\Database\Query\Builder::attendance()"

arukomp's avatar
    $attendances = auth()->user()->attendances()->paginate(10);

    return view('/dashboard')->with('attendances', $attendances);

try this

1 like
rin4ik's avatar

what you get now? do you have attendance relation? maybe attendances?

 $user = User::where('id', $user_id)->attendances()->paginate(10);
dd($user);

or

 $attendences = User::where('id', $user_id)->attendances()->paginate(10);
 return view('/dashboard',compact('attendences'));
1 like
erikverbeek's avatar

When you're accessing a relationship, you don't need the (). Another quick tip, auth()->user() already gives you the user you are trying to pull from the DB.

So I think al you need might be:

return view('/dashboard')->with('attendances', auth()->user()->attendance->paginate(10);
1 like
blitzkin's avatar

@rin4ik

still same error

attendance model

class Attendance extends Model { //

public function user(){
    return $this->belongsTo('App\User');
}

user model

public function attendances(){
    return $this->hasMany('App\Attendance');
}

@arukomp

thanks this works, but if it's ok i prefer the original code since i really new to laravel, but thanks again the code you provided works!! :)

1 like
rin4ik's avatar
rin4ik
Best Answer
Level 50

@blitzkin no problem here use this. your mistake was you don't have attendance you have attendances instead

 $attendences = User::where('id', $user_id)->attendances()->paginate(10);
 return view('/dashboard',compact('attendences'));
1 like
blitzkin's avatar

in line with the original question,

is there a way to display the last pagination page instead of the default first page upon loading the view?

thanks in advance guys!

blitzkin's avatar

hi @rin4ik,

i tried appending it in the view statement but its not working..

return view('/dashboard')->with('attendances', $attendances->lastPage());

sorry for the noob question..

blitzkin's avatar

@rin4ik bcoz I want the users to see the latest records vs using the orderBy asc-desc.

for example:

nov1 - 5:00PM - 5:30PM

nov2 - 5:00PM - 5:35PM

nov3 - 5:00PM - (no record at the moment)

this is actually what i want and is displaying fine.

the only problem is that these entries are at the last page of pagination (asc default of pagination i guess).

so i'm thinking if there is a way to just show the last page when users logs in.

hope I explained it clear :)

rin4ik's avatar

@blitzkin use this instead :)

 $attendences = User::where('id', $user_id)->attendances()->latestFirst()->paginate(10);

in your User model create scopelatestFirst

 public function scopeLatestFirst($query)
    {
        return $query->orderBy('created_at', 'desc');
    }
1 like
blitzkin's avatar

@rin4ik thanks much.. i'll try to implement this, hope i will not get confuse lol.. thanks again.. :)

Please or to participate in this conversation.