EvergreenWebSystems
5 months ago
100
4
Laravel

Beginner routes and controllers query

Posted 5 months ago by EvergreenWebSystems

I am new to Laravel so sorry if I have missed some obvious concepts with this question. I am working my way through 'Laravel 5.7 From Scratch' and am just seeking some clarification about route and controllers.

If I have a project where I know a joined item is going to be used widely throughout the database, i.e. Projects will have tasks, Clients will have tasks, Enquiries will have tasks etc... Should I use this approach to generate my web routes:

Route::resource('projects/{project}/tasks', 'ProjectTasksController');
Route::resource('clients/{client}/tasks', 'ClientTasksController');

Because if I try to shorten the URI, then I can do this for projects:

Route::resource('tasks', 'ProjectTasksController');

But then I am stuck, i.e. I cannot reuse the tasks route for client tasks because it has already been used.

If that above concept is correct in my ProjectTasksController I add:

use App\Task;
use App\Project;

And then in order to store or update a task which just has a description and a checkbox I do this,

    public function store(Project $project, Task $task)
    {
        $project->tasks()->create(request()->validate([
            'description' => ['required', 'min:3'],
        ]));

        return back();
    }

    public function update(Project $project, Task $task)
    {
        $task->update(request()->validate([
            'description' => ['required', 'min:3'],
            'completed' => ['boolean'],
        ]));

        return back();
    }

I have tried to follow the advice in the Laracasts and write my functions as succinctly as possible. Am I right to use (Project $project, Task $task) like that?

Sorry again if this is obvious to seasoned Laravel users, I am just trying to get to grips with the framework.

Thanks

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