3
votes

I have tables: users, resources and pivot table user_resources.

users

  • id
  • username

etc.

resources

  • id
  • resource_name

etc.

user_resources

  • id
  • user_id
  • resource_id

User model:

class Resources extends Eloquent {

    public function users() {
        return $this->belongsToMany('User', 'user_resources');
    }
}

and resource model:

class User extends Eloquent {

    public function resources() {
        return $this->belongsToMany('Resource', 'user_resources');
    }
}

How can I get all of the users and all of the resources that belongs to the users.

1

1 Answers

7
votes

Probably you just have to:

$everything = User::with('resources')->get();