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">×</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"
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
– Gustavo_Tavares
What controller name is this public Function store(Request $request, $id)? Its name is Taskcontroller?
– Renato Tavares
Yes, that very name Taskcontroller.
– Gustavo_Tavares
test the question Edit, let’s see, if not solve I delete my answer to another try to help you
– Renato Tavares
Show! It worked, just a detail, the method is POST and not GET. But I appreciate the help guy.
– Gustavo_Tavares
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...
– cau