The second option is the right one : you need a pivot table. You don't need any other controller or model. The relationship is defined like you did for the first option.
// User model
public function tasks()
{
return $this->belongsToMany(Task::class);
}
To retrieve all tasks belonging to a user, you just have to use the defined relationship.
$tasks = $user->tasks;
And you can do the same for the Task model.
// Task model
public function users()
{
return $this->belongsToMany(User::class);
}
To retrieve all users for a specific task, you just have to use the defined relationship.
$users = $task->users;
You will probably have to customize the table name.
return $this->belongsToMany(Task::class, 'user_task');
...
return $this->belongsToMany(User::class, 'user_task');