Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kashifullah's avatar

Relationship between 3 tables

I am working on Project Management, and I can't figure out how to links 3 tables in relationships, the tables are as follow:

Users

  • name
  • email
  • password

Projects

  • name
  • description

Tasks

  • title
  • due_date

Pivot Table : project_task_user

  • user_id
  • task_id
  • project_id

So these 3 tables having many to many relationship with one another, i.e. Users can have many tasks, and Projects can have many tasks, also tasks can belongs to a user or a project, could anyone please guide me to the right path. Thank you

0 likes
14 replies
InaniELHoussain's avatar

well the only possible thing you can do is to create a Model for it, set the $fillable fields and set the relationships .... a pivot table only accepts two tables's key.

tykus's avatar

Can a Task belong to many Projects, and can a Task belong to many Users? If not, then it's presence on the pivot table might be superfluous.

kashifullah's avatar

Thank you @InaniELHoussain, I have already created my models for each table, except pivot table, if I can't link more than two keys could you please guide me further what else I can do?

kashifullah's avatar

Thank you, @tykus_ikus. Can a Task belong to many Projects? No. Task belong to many Users? No.

Could you clarify it more

tykus's avatar
tykus
Best Answer
Level 104

So in that case, the tasks table should become:

title
due_date
user_id  // you could make this to owner_id and be explicit with the foreign key when defining the relationships
project_id
1 like
kashifullah's avatar

That make sense, but I found that the tasks table should be separate from other tables and the pivot table would be needed, Am I right or Is that making sense?

tykus's avatar

The need for the pivot table would depend on whether or not a User can belong to many Projects; and a Project can belong to many Users.

1 like
kashifullah's avatar

Thank you @tykus_ikus I have already create that pivot table separate, named project_user having two fields user_id, project_id and therefore, user can belong to many projects; also project can belong to many users. :)

tykus's avatar

@kashifullah you should be all set in that case.

Be aware though that you will now have users attaching to a project in two ways (i) using the project_user pivot table and (ii) using the tasks table - this may or may not be an intended behaviour.

kashifullah's avatar

@tykus_ikus What if I implement the same from tasks table rather than project_user pivot table, which is more preferred way to do so.

tykus's avatar

If your project will only have project participant, i.e. user has task(s), rather than having project watchers, you could remove the pivot table completely.

kashifullah's avatar

@tykus_ikus one final question, how can I insert values to tasks tables as it has two foreign keys, user_id and project_id, could you please show me that in php artisan tinker form, let say I have a one-to-many relationship between users and tasks thus I have a relationship method in task table as: public function user() { return $this->belongsTo('App\User'); } And therefore, I can call it as : $task -> user()->attach($user);

But how to insert project_id in task table?

tykus's avatar

Think in terms of the relations:

$user = App\User::findOrFail($request->user_id);
$project = App\Project::findOrFail($request->project_id);

$task = new App\Task([
    'title' => $request->title,
    'due_date' => $request->due_date
]);

$task->associate( $user ); // sets the user_id

$project->tasks()->save($task); // sets the project_id and saves the new task
1 like

Please or to participate in this conversation.