<td> <form action={{ route('task.status', [$task]) }} method="POST"> {{ csrf_field() }} <button type="submit">check</button> </form> </td> 

This is the button itself.

  Route::post('/', ['middleware' => 'auth', 'uses' =>'PostController@check_box'])->name('task.status'); 

This is the route

 public function check_box(Task $task) { dd($task->name); if ($task->is_active == true) { dd($task->is_active); $task->is_active = false; $task->save(); }else{ $task->is_active = true; $task->save(); } return redirect('/'); } 

This is the controller itself + I have a model so that the task variable comes out of the database

  public function boot() { Route::model("task",Task::class); parent::boot(); } 

the button on dd in the controller gives out any request for information id, name, etc. gives out null, tell me what is wrong and if there is an article on this issue I will gladly read it, I haven’t found anything like it

  • Does the variable have a value in the template? - Anton Kucenko pm

1 answer 1

Please, when writing code, follow the recommendations of clean code (write class methods in camelCase ).

Route Model Binding works under several conditions:

  1. Define the parameter in the route:
 Route::post('/{task?}', ['middleware' => 'auth', 'uses' =>'PostController@checkBox'])->name('task.status'); 
  1. In the parameters of the controller, define a variable with the name that corresponds to the name of the parameter in the route definition
 // /{task?} public function checkBox(?Task $task = null){ dd($task) } 
  • I thank for the advice, I'm just learning - Nikita Kalaydin