Problem passing parameter in form | Laravel

Asked

Viewed 379 times

0

I have an application for project management, and each project has its tasks. These tasks can be deleted or completed.

Each task is displayed through a foreach, which is within a view, being displayed within a card, and within 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 conclusion or deletion and when confirming, I call the controller (Taskcontroller).

However, when I do any of the actions, I get the same mistake: 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 the 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">&times;</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 (performs 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 am using the route resourcer of Laravel; I tried to leave the modal on the same page where the cards of tasks are being displayed; I also noticed that when I click the button, I can’t even reach the controller;

  • Your code has problems, you are using wrong the Eloquent and values may be sending wrong information to Views. Just one observation: if you need to filter the model Task do before calling the database command, if not you bring all the contents of the tables to then filter and this is a problem.

  • You could tell where the mistake is?

  • $task = Task::all()->where('project_id', '=', $id); here for example

  • I’m asking because I have no idea how to do this, but how do I filter the items without even making the query in the bank?

  • should be $task = Task::where('project_id', '=', $id); and if you need to carry $task = Task::where('project_id', '=', $id)->get();

  • cool, did so, the result was the same. Is this a good practice? Or is it due the first time I have loaded everything and then made the filters? We lost in performance?

  • the result is the same, but the optimization in the return is such, so you do the filter directly in SQL and not in a class that loads the entire table !!! that’s the difference in performance...

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.