phplaravelmodeleloquentobject-relational-model

Laravel Eloquent - accessing second level model relationship


This question extends the example Eloquent : Working With Pivot Tables provided in the Laravel Documentation.

The relationship here is that a User has many Role objects that it can relate to. In this extension, each Role will relate to a number of Task objects, giving us a second level many-to-many relationship.

Using Eloquent ORM, what would be the neatest way of accessing the Tasks that a user relates to?

Specifically, the following method should return an array of task_ids

User::get_tasks($user_id)

Solution

  • Even though @JosephSilber's answer looks great it unfortunately didn't work when I tested it, so here's something that worked on my installation:

    public static function get_tasks($id){
        $tasks = static::with('roles.tasks')->find($id)->roles->lists('tasks');
        $collection = new \Illuminate\Database\Eloquent\Collection();
        foreach($tasks as $roleTasks){
            $collection = $collection->merge($roleTasks);
        }
        return $collection;
    }
    

    Personally I'd change the syntax a bit to this:

    public function getTasks(){
        $this->load('roles.tasks');
        $tasks = $this->roles->lists('tasks');
        $collection = new \Illuminate\Database\Eloquent\Collection();
        foreach($tasks as $roleTasks){
            $collection = $collection->merge($roleTasks);
        }
        return $collection;
    }
    
    User::find($user_id)->getTasks();