Pass parameter to route resource | Laravel

1

I'm developing a project control application. Each project has its tasks, that is, when I create a task, it needs to be linked to the project. I'm using the Laravel feature of Route resource. When I pass the project id to the route, it does not identify when it enters the store method.

task creation form:

<div class="modal fade" id="tarefa" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="exampleModalLabel"><i class="fa fa-edit"></i> Nova tarefa</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.store', ['id' => $project->id]) }}" method="POST">
                    @csrf
                    <div class="form-group">
                        <label for="name">Nome da tarefa</label>
                        <input name="name" type="text" class="form-control" id="name" placeholder="Nome">
                    </div>

                    <div class="form-group">
                        <label for="description">Descrição da tarefa</label>
                        <textarea name="description" type="Description" class="form-control" id="description" placeholder="Descrição"></textarea>
                    </div>

                    <div class="form-group">
                        <label for="difficult">Dificuldade</label>
                        <select class="form-control" name="difficult">
                            <option value="0">- Selecione -</option>
                            <option value="1">Fácil</option>
                            <option value="2">Média</option>
                            <option value="3">Difícil</option>
                        </select>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Sair</button>
                        <button type="submit" class="btn btn-primary">Cadastrar</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

Task method of the TaskController class:

public function store(Request $request, $id)
{
    $request->merge([
       'project_id' => $id
    ]);
    Task::create($request->all());
    return back();
}

Error message: "Too few arguments to function App\Http\Controllers\TaskController::store(), 1 passed and exactly 2 expected"

    
asked by anonymous 27.11.2018 / 01:40

1 answer

2

In the Laravel documentation available , the controller does not exemplify this parameter Request $request it me seems to be transparent to resource controllers.

Your error basically says that you are only passing one parameter in the url but the method expects 2 parameters. If you remove Request $request , does not the error disappear?

Check out the Laravel documentation for how to create the Task using filters or something else outside of that controller

EDIT: Another solution and idit your route file and pass the parameter there

Route::get('/store/{id}', 'TaskController@store');
    
27.11.2018 / 01:48