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
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.
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?
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?
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. :)
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.
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.
@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);