Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I was trying to get the attendance of each user in a course where the users and the courses have many to many relationship and the attendance table is based on the relationship. But i couldn't call $user->pivot->attendance(); directly i had to call for the pivot object it self so i can call a function to it as you can see in the answer

The Following is a sample of my Database scheme

I need to run

$users=$course->users;
foreach($users as $user)
    $user->pivot->attendance(); //error in this line

this line gives and error

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

users

courses

startDate endDate

course_user

course_id user_id payment regDate status

userattendance

course_user_id inOrOut

And here is the CourseUserPivot Class

class CourseUserPivot extends Eloquent {
    protected $table ="course_user";
    public function course()
        return $this->belongsTo('Course'); 
    public function user()
        return $this->belongsTo('User'); 
    public function attendance()
        return $this->hasMany('Userattendance','course_user_id');

P.S: $user->pivot->payment works and displays the attribute but i cant call methods

You can't use $user->pivot->attendance(); as this calls attendance on the user object not on the pivot object

You will have to fetch the pivot object then call the function like so

CourseUserPivot::find($user->pivot->id)->attendance();

make user that where you used withPivot() function you include the id in the array

like in

class Course{
public function users()
        return $this->belongsToMany('Candidate')
                    ->withPivot('id',
                    'summary',
                    'status',
                    'payment'
                It's strange that I'm first guy who +1 this Q/A. thanks to you but I hope if there is a better way since in my case I'm calling this function from within views
– Mohamed Mo Kawsara
                Jan 23, 2017 at 12:44
                @MohamedKawsara you can call it in the controller $courses->with('users'); and in the view just call foreach on $courses and then foreach on  $course->users as $user normal eagerloading
– Bakly
                Jan 25, 2017 at 11:41

I was trying to accomplish the same thing, and I was having trouble getting it to work like I wanted until I included the 'id' column of my custom pivot model in the withPivot() method.

For some context, my database structure is:

  • User table
  • Items Table
  • Sets table (defines a set and the items that can be in an instance of it)
  • Set_User table (Custom Pivot Model, instances of a set created by a user)
  • id, set_id, user_id
  • Items_SetUser table (items that belong to a set instance that belong to a user)
  • item_id, setuser_id
  • I do this in the controller to get all the sets that user has made an instance of, with custom pivot table data:

    $user_sets = \Auth::user()->sets()->withPivot('id', 'name')->get();
    

    And then in blade I can access the items associated with each set instance like this:

    $set->pivot->set_items()->count()
    

    Without including 'id' in the call to withPivot(), the above is not possible. Note that set_items() is a method on the custom pivot model that defines a relationship with the items table.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.