Pass parameter to route Resource | Orange

Asked

Viewed 961 times

2

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 am using the Route Laravel feature. When I pass the project id to the route, it does not identify when entering 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>

store 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"

1 answer

2


In the documentation of Laravel available here, in the controller this parameter is not exemplified Request $request it seems to me to be transparent to the Resource controllers.

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

Check out the Laravel documentation to learn how to create Task using filters or something outside of this controller

EDIT: Another solution is to choose your route file and pass the parameter there

Route::get('/store/{id}', 'TaskController@store');
  • if I do this, I don’t pass parameter to Task::create(). But anyway, I removed and passed, just to see if it captured the project id, but it also didn’t, the error message now was Too few Arguments to Function App Http Controllers Taskcontroller::store(), 0 passed and Exactly 1 expected

  • What controller name is this public Function store(Request $request, $id)? Its name is Taskcontroller?

  • Yes, that very name Taskcontroller.

  • test the question Edit, let’s see, if not solve I delete my answer to another try to help you

  • Show! It worked, just a detail, the method is POST and not GET. But I appreciate the help guy.

  • Friend, I believe the store has no id parameter.. And on Edit, the parameter is from the task... The project id goes in the form. Then you can validate this id in Taskrequest ... da to check if this id exists in the projects table. You have to drop a select of all projects in the form...

Show 1 more comment

Browser other questions tagged

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