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
try now:)
$user = User::where('id', $user_id)->attendance()->paginate(10);
hi tnx for the reply..
still the same error:
"Call to undefined method Illuminate\Database\Query\Builder::attendance()"
$attendances = auth()->user()->attendances()->paginate(10);
return view('/dashboard')->with('attendances', $attendances);
try this
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'));
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);
@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!! :)
@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'));
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!
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..
@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 :)
@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');
}
@rin4ik thanks much.. i'll try to implement this, hope i will not get confuse lol.. thanks again.. :)
@blitzkin try and let me know if it worked :)
Please sign in or create an account to participate in this conversation.