1
votes

Laravel (5) newbie question. Building Projects / Tasks project.

Routes:

Route::resource('project', 'ProjectsController');
Route::resource('project.task', 'TasksController');

Projects model:

public function tasks()
{
    return $this->hasMany('App\Tasks');
}

Tasks model:

public function project()
{
    return $this->belongsTo('App\Projects');
}

Now trying to edit/update specific task. Route to task is project/{project}/task/{task}/edit

** Question - Are my Edit and Update functions in TasksController correct? It works, but not really sure if all should be done that way... **

public function edit($project_id, $task_id)
{
    $Task = Tasks::find($task_id);
    return view('task_edit', compact('Task'));
}

public function update($project_id, $task_id)
{
    $input = array_except(Input::all(), '_method');
    $Task = Tasks::find($task_id);
    $Task->update($input);

    return Redirect::route('project.show', array($Task->project->id));
}
1
If you've assigned a $fillable array on your Task model then you don't need to remove _method from $input, you can send Input::all () and only the fillable columns will be updated. You should probably implement some validation, too. - aethergy
Thanks. Will try with Input::all() only. And yes, need to implement validation. What about $project_id parameter. I'm confused whether $project_id parameter which is passed to both edit / update (also create and store) functions should be somehow used or not? - Tomas

1 Answers

0
votes

You dont have any validation. Not sure if intended or not. You can use Request::except('field'); to get all values except unwanted one. other than that Things look good. Is there anything you are concerned about?