I have an application for project management, and that each project has its tasks. These tasks can be deleted or completed.
Each task is displayed through a foreach, which is inside a view, being displayed inside a card, and inside this card, has two buttons, one for each action.
To load this page, it is called through a controller (ProjectController) and when clicking the buttons, I call a modal to confirm such completion or deletion and when I confirm, I call the controller (TaskController).
However, when I do any of the actions, I get the same error: Undefined variable: task_id (View: C:\Users\User\project-manager\resources\views\modals\modals.blade.php) (View: C:\Users\User\project-manager\resources\views\modals\modals.blade.php)
ProjectController (displays page with tasks):
public function show($id)
{
$task = Task::all()
->where('project_id', '=', $id);
$task_id = Task::all()
->where('project_id', '=', $id)
->first();
$task_count = count($task);
$t = $task->where('final_date', '!=', null);
$task_done = count($t);
$task_not_done = $task_count - $task_done;
return view('project.edit', ['project' => Project::findOrFail($id)])
->with('task', $task)
->with('task_count', $task_count)
->with('task_done', $task_done)
->with('task_not_done', $task_not_done)
->with('task_id', $task_id);
}
Modal for completion / exclusion of tasks:
<!-- Modal to check a task -->
<div class="modal fade" id="check" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="exampleModalLabel">Atenção!</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="{{ route('tasks_update', $task_id->id) }}" method="PUT">
<p>
Você deseja concluir esta tarefa?
</p>
@csrf
<input name="code" id="code" type="hidden" value="1">
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Sair</button>
<button type="submit" class="btn btn-success">Concluir</button>
</div>
</form>
</div>
</div>
</div>
</div>
Route: Route::put('tasks/{id}', 'TaskController@update')->name('tasks_update');
TaskController (perform update):
public function update(Request $request, $id)
{
if($request->input('code') == 1){
DB::table('tasks')
->where('id', '=', $id)
->update(['check' => 1]);
return view()->route('projects.show');
}
if($request->input('code') == 2) {
DB::table('tasks')
->where('id', '=', $id)
->update(['deleted_at' => now()]);
return view()->route('projects.show');
}
}
Note: I'm using Laravel's route resourcer; I already tried to leave the modal on the same page where the task cards are being displayed; I also noticed that clicking the button, I can not even get to the controller;