I'm making a small laravel project to implement Eloquent Relationships, I have multiple models mainly (Project, Task, User, File, etc)
And a Project can have multiple users assigned, multiple files attached to it, and can also have multiple tasks. And a single Task can have multiple users assigned and can also have multiple files assigned, along with multiple other things. I have googled and implemented hasManyThrough, and belongsToMany relationships but I get confused a lot with relations. Any help?
Project.php
class Project extends Model
{
protected $fillable = ['name', 'user_id'];
public function users()
{
return $this->belongsTo(User::class);
}
public function tasks()
{
return $this->hasManyThrough(Task::class, User::class);
}
}
Task.php
public function user()
{
return $this->belongsTo(User::class);
}
User.php
public function tasks()
{
return $this->hasMany(Task::class);
}
public function project()
{
return $this->belongsTo(Project::class);
}
The error I get is when I go to projects/1 I get multiple tasks attached to them but not multiple users.