Laravel Eloquent: Bind Table to another table via pivot with multiple relationships

ogjtech

Let me start by apologizing for the vague title, I couldn't find a proper way to formulate it.

As for the question, in this instance I want to bind users to a project with a certain role. I'm trying to do this via the following pivot table:

|user_has_projects|
|-----------------|
|id               |
|user_id          |
|projectrole_id   |
|project_id       |
|created_at       |
|updated_at       |
|deleted_at       |
|-----------------|

The users, projects and projectroles tables are what you would expect from said tables.

The current Eloquent relationships per model are as follows:

Project.php:

public function users()
 {
   return $this->belongsToMany(
      "App\User",
      "user_has_projects"
   )->withPivot(
      "user_id"
   )->withTimestamps();
}

public function userProjects()
{
    return $this->hasMany(
        "App\UserProjects",
        "project_id",
        "id"
    );
}

Users.php:

public function projects()
 {
   return $this->belongsToMany(
      'App\Project',
      'user_has_projects'
   )->withPivot(
      'project_id'
   );
}

public function projectRole($id)
{
    return $this->belongsToMany(
        "App\ProjectRole",
        "user_has_projects"
    )->withPivot(
        "projectrole_id"
    )->wherePivot(
        "project_id",
        '=',
        $id
    );
}

ProjectRole.php:

public function user()
{
    return $this->belongsToMany(
        "App\User",
        "user_has_projects"
    )->withPivot(
        "user_id"
    )->withTimestamps();
}

public function projects()
{
    return $this->belongsToMany(
        "App\Project",
        "user_has_projects"
    )->withPivot(
        "project_id"
    )->withTimestamps();
}

UserProjects.php:

public function projectRoles()
{
    return $this->belongsTo(
        'App\ProjectRoles'
    );
}

public function users()
{
    return $this->belongsTo(
        'App\User'
    );
}

public function projects()
{
    return $this->belongsTo(
        'App\Project'
    );
}

Now if I want to retrieve all the users bound to a project and then their roles in the project I would first call $aBoundUsers = $oProject->users(); and then loop through those users to get their roles with

foreach($aBoundUsers as $oUser) {
$role = $oUser->projectRole($oProject->id); 
}

However, when I call $oUser->projectRole(...) I get the following error: "Call to a member function projectRole() on boolean".. After some thorough searching I found out that the only thing returned is {"withTimestamps":true}

What am I doing wrong here? I found some solutions on having pivot tables with multiple models, but none work somehow.

Jonas Staudenmeir

$oProject->users() only returns the relation, $oProject->users returns the result.

Change the parent class of your UserProjects model:

class UserProjects extends \Illuminate\Database\Eloquent\Relations\Pivot

Then adjust your Project model:

public function users()
{
    return $this->belongsToMany(
        "App\User",
        "user_has_projects"
    )->withPivot(
        "user_id", "projectrole_id"
    )->using("App\UserProjects")
    ->withTimestamps();
}

Then you can access your roles like this:

foreach($oProject->users as $oUser) {
    $role = $oUser->pivot->projectRoles; 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel eloquent relationships with pivot table

multiple pivot table laravel eloquent

Laravel: How to use multiple pivot table relationships

Laravel eloquent with pivot table

Laravel Eloquent - Pivot Table

Polymorphic relationships via a pivot table

laravel nested relationships pivot table

Eloquent, multiple relationships per table

Laravel/Eloquent: condition on a pivot table

hasOneThrough with pivot table laravel eloquent

Laravel Eloquent - querying pivot table

How to query pivot table with multiple foreign keys Laravel 5 Eloquent

Laravel Eloquent for pivot table with 2 foreign keys to a table and 1 foreign key to another table

Complex relationships in Laravel (hasManyThrough through a pivot table)

Using relationships in Laravel Eloquent with bridge table

Laravel Eloquent - Using pivot table's id as a foreign key in another table

Eloquent Table Relationships

Getting count from pivot table in laravel eloquent

Laravel Eloquent group by with pivot table & relations

How to query count on the pivot table in Laravel eloquent

Laravel Eloquent Migrate and Seed pivot table

Connect pivot table with multiple tables in Eloquent

Laravel Relationships with multiple eloquent

Laravel Eloquent ManyToMany with Pivot, secondary query with variables in pivot table?

Laravel, Eloquent table relating to itself through pivot table

Eloquent Query with Pivot Table

It is possible to relate a pivot table to another pivot table in laravel 5.5?

Eloquent relation pivot table with table

Multiple relationships on same table with two column Laravel