Laravel Eloquent Many to Many relationship using a pivot table

Tom Bird

I'm working on creating my own Project Manager, I currently have a projects, users and a project_users table. I am trying to create the relationships in the models to associate one project to many users, but I have never created a many to many relationship. I looked at the documentation and tried various examples but none worked for me.

Here is my ProjectController:

public function single( $id ) {

$project = Project::with('client','projecttypes','storys', 'sprints', 'projectusers')->find( $id )->toArray();

return View::make('projects.single', array( 'project' => $project ) );

}

This is simply grabbing a single project and all of the related models and returning them to the view.

My Project model:

class Project extends Eloquent {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'projects';

    protected $fillable = array( 'client_id', 'title', 'description', 'type_id', 'status', 'hours_estimated', 'hours_actual', 'date_estimated', 'date_due', 'date_completed', 'created_at', 'updated_at' );

    public function projectusers() {
        return $this -> hasMany( 'User', 'id', 'user_id' );
    }

}

My users model:

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array( 'password', 'remember_token' );

    protected $fillable = array( 'client_id', 'title', 'first_name', 'last_name', 'address', 'city', 'state', 'zip', 'email', 'password', 'status', 'created_at', 'updated_at', 'remember_token', '_token' );

    public function project() {
        return $this -> belongsToMany('Project');
    }

}

and finally my ProjectUsers model:

class ProjectUsers extends Eloquent {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'project_users';

    protected $fillable = array( 'project_id', 'assigner_id', 'user_id', 'notifications', 'created_at', 'updated_at' );

    public function project() {
        $this -> belongsTo('Project');
    }

}

All fields in the database are listed in the models $fillable with the exception of the primary id id.

I just need to figure out how to get all users assigned to a specific project utilizing the pivot table, any help would be greatly appreciated!

Thanks

Tom Bird

I got an answer on a Laravel Forum, here it is:

ProjectController

$project = Project::with('client','projecttypes','storys', 'sprints', 'users')->find( $id )->toArray();

Project model

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

User model

public function projects() {
    return $this -> belongsToMany('User');
}

This worked for me!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel Eloquent many-to-many relationship: Use explicit pivot table

From Dev

Laravel many to many relationship using Eloquent

From Dev

Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

From Dev

Laravel - Order by pivot value in Many to Many table relationship

From Dev

Unable to sync conditional pivot table many to many relationship laravel

From Dev

Laravel L5.5 No access to pivot table in "Many to many" relationship

From Dev

How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

From Dev

Laravel eloquent one to many relationship in custom pivot tables

From Dev

Eloquent - Many to many where not found in the pivot table

From Dev

Eloquent many to many relationship access table column value from pivot table

From Dev

Laravel Removing Pivot data in many to many relationship

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Creating one to many relationship with pivot table of many to many relationship

From Dev

Creating one to many relationship with pivot table of many to many relationship

From Dev

Soft deleting a many-to-many relationship's pivot in Eloquent

From Dev

Soft deleting a many-to-many relationship's pivot in Eloquent

From Dev

Is there a way to select columns using eloquent one to many relationship in laravel 5.2?

From Dev

How to get data for one to many relationship using Eloquent in Laravel?

From Dev

Laravel 4 - Many to Many - pivot table not updating

From Dev

Laravel Many-to-Many Pivot Table

From Dev

Laravel Eloquent many to many filter on intermediate table

From Dev

Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

From Dev

Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5

From Dev

Laravel 4.2 Many-to-many relationship : Can't read from pivot table

From Dev

laravel update, remove one to many relationship eloquent

From Dev

Laravel eloquent update one-many relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel eloquent update one-many relationship

From Dev

Laravel 5 one to many eloquent relationship

Related Related

  1. 1

    Laravel Eloquent many-to-many relationship: Use explicit pivot table

  2. 2

    Laravel many to many relationship using Eloquent

  3. 3

    Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

  4. 4

    Laravel - Order by pivot value in Many to Many table relationship

  5. 5

    Unable to sync conditional pivot table many to many relationship laravel

  6. 6

    Laravel L5.5 No access to pivot table in "Many to many" relationship

  7. 7

    How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

  8. 8

    Laravel eloquent one to many relationship in custom pivot tables

  9. 9

    Eloquent - Many to many where not found in the pivot table

  10. 10

    Eloquent many to many relationship access table column value from pivot table

  11. 11

    Laravel Removing Pivot data in many to many relationship

  12. 12

    Laravel 5 Eloquent count many to many relationship

  13. 13

    Creating one to many relationship with pivot table of many to many relationship

  14. 14

    Creating one to many relationship with pivot table of many to many relationship

  15. 15

    Soft deleting a many-to-many relationship's pivot in Eloquent

  16. 16

    Soft deleting a many-to-many relationship's pivot in Eloquent

  17. 17

    Is there a way to select columns using eloquent one to many relationship in laravel 5.2?

  18. 18

    How to get data for one to many relationship using Eloquent in Laravel?

  19. 19

    Laravel 4 - Many to Many - pivot table not updating

  20. 20

    Laravel Many-to-Many Pivot Table

  21. 21

    Laravel Eloquent many to many filter on intermediate table

  22. 22

    Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

  23. 23

    Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5

  24. 24

    Laravel 4.2 Many-to-many relationship : Can't read from pivot table

  25. 25

    laravel update, remove one to many relationship eloquent

  26. 26

    Laravel eloquent update one-many relationship

  27. 27

    Laravel 5 Eloquent Relationship - Has Many Though

  28. 28

    Laravel eloquent update one-many relationship

  29. 29

    Laravel 5 one to many eloquent relationship

HotTag

Archive