Home > Mobile >  Laravel many to many relationship with pivot
Laravel many to many relationship with pivot

Time:08-30

I'm using Laravel Filament.

I got a projects and responsibles tables in a many-to-many relationship. But also another table of responsabilityTypes

  • projects

    • id
    • title
  • responsibles

    • id
    • name
  • responsabilityTypes

    • id
    • name
  • project_responsible

    • project_id
    • responsible_id
    • responsibilityType_id

And here are my relationships setup:

Responsible.php

public function projects() {
    return $this->belongsToMany(Project::class,'rel_project_responsible','responsible_id','project_id')
    ->withPivot('responsibilityType_id')
    ->withTimestamps()
    ->using(AcademicoProyecto::class);
}

Project.php

public function responsibles() {
    return $this->belongsToMany(Responsible::class,'rel_project_responsible','project_id','responsible_id')
    ->withPivot('responsibilityType_id','sort')
    ->withTimestamps()
    ->using(AcademicoProyecto::class);
}

I have set up a class for the pivot table like so:

ProjectResponsible.php

use Illuminate\Database\Eloquent\Relations\Pivot;

class AcademicoProyecto extends Pivot
{

}

ResponsibilityType.php

//Don't know how to set up

My question is, when the user is in a Project Edit page and clicks on the "attach" button, in order to add a Responsible record, a Modal pops up to select a Responsible, but I also implemented a Select list to display the different types of responsibilities.

What am I missing to set up in order to access and display the types of responsibilities in the select list and attach it to the pivot table?

CodePudding user response:

Your question asks about "access and display" but you have no controller or view code. But for the model, it's just a simple relationship between two tables, so define it as such:

class AcademicoProyecto extends Pivot
{
    use SoftDeletes;

    public function responsibilityType() {
        return $this->belongsTo(ResponsibilityType::class);
    }
}

class ResponsibilityType extends Model
{
    protected $fillable = ["name"];
}

Now you simply update the other models to access the relationship in the withPivot() call.

class Responsible extends Model {
    public function projects() {
        return $this->belongsToMany(Project::class,'rel_project_responsible','responsible_id','project_id')
        ->withPivot('responsibilityType')
        ->withTimestamps()
        ->using(AcademicoProyecto::class);
    }
}

class Project extends Model {
    public function responsibles() {
        return $this->belongsToMany(Responsible::class,'rel_project_responsible','project_id','responsible_id')
        ->withPivot('responsibilityType', 'sort')
        ->withTimestamps()
        ->using(AcademicoProyecto::class);
    }
}

Now you should be able to do, for example:

$foo = Responsible::with("projects")->first();
foreach ($foo->projects as $project) {
   echo $project->pivot->responsibilityType?->name;
}
  • Related