lilo
1 month ago

How to access data through the pivot table

Posted 1 month ago by lilo

Hello, I have a many to many relationship and naturally a pivot table.

Here is my Project model:

public function dev_names()
{
    return $this->belongsToMany(Developer::class, 'developer_projeler','developer_id','projeler_id');
}

Here is my Project controller:

$project = Projeler::find(1);

    foreach ($project->dev_name as $dev_name) {
        $dev_name->pivot->developer_id;
        dd($dev_name);
    }

How can I access to the projects who has developers inside?

With the code above in controller I get this error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (database.developer_projeler, CONSTRAINT projeler_id_fk_2211026 FOREIGN KEY (projeler_id) REFERENCES projelers (id) ON DELETE CASCADE) (SQL: insert into developer_projeler (developer_id, projeler_id) values (39, 8))

Please sign in or create an account to participate in this conversation.