Controller method getting wrong value in Laravel

Asked

Viewed 49 times

0

A piece of my application, consists of the client logging in and from there registering some relevant information daily. When logging in, it drops to the screen to choose which capture it will log the data into. Next time he chooses which capture system, and finally he inserts the data. For this I have the following routes:

Route::get('/prop/{id_prop}/', 'PropriedadeController@clientePropriedade');
Route::get('/prop/{id_prop}/cap/{id_cap}/', 'CaptacaoController@clienteCaptacao')->name('cap');
Route::get('/prop/{id_prop}/cap/{id_cap}/sis/{id_sis}/', 'SistemaController@clienteSistema')->name('sis');

My problem is, when I click on the link to some capture, I’m always directed to the Capture System page belonging to capture 1. Regardless of the capture I choose, I’m always directed to the capture system 1. Using Laravel’s dd() method to debug, I realized that the parameter (int id) of my client methodCaptacao de Captacaocontroller has value 1.

 public function clienteCaptacao(int $id)
 {
    dd($id);//valor 1
    $cap = Captacao::find($id);

    //dd($cap);
    if (!isset($cap)) {
        return view('erros.cliente.nCadastrado',
            array('nome' => 'captações'));
    } else {
        return view('cliente.escolheSistema', compact('cap'));
    }
 }

The links are correct:

 <div class="col-lg-3 col-md-6">
    <a href=http://h20.laravel/cliente/prop/1/cap/1 
        class="btn  btn-secondary btn-block" style="background-color:red">Captacao 1</a>
 </div>
 <div class="col-lg-3 col-md-6">
    <a href=http://h20.laravel/cliente/prop/1/cap/2 
        class="btn  btn-secondary btn-block" style="background-color:red">Captacao 2</a>
 </div>
 <div class="col-lg-3 col-md-6">
    <a href=http://h20.laravel/cliente/prop/1/cap/3 
        class="btn  btn-secondary btn-block" style="background-color:red">Captacao 3</a>
 </div>

These are the codes from my Slide where I create the links above:

<div class="row">
   @foreach ($prop->captacoes as $cap)
     <div class="col-lg-3 col-md-6">
        <a href={{
           URL::route("cap",
            ['id_prop'=>$prop->id, 'id_cap'=>$cap->id])}} 
                class="btn  btn-secondary btn-block" style="background-color:red">{{$cap->nome}}</a>
    </div>
   @endforeach
</div>

2 answers

1


Since your route contains two parameters, it is the first parameter that is being injected into your method, in this case, {id_prop}. See the description on the site

Route Parameters are Injected into route callbacks / controllers based on their order - the Names of the callback / controller Arguments do not Matter. https://laravel.com/docs/5.6/routing#required-Parameters

To solve your problem, your method clienteCaptacao should receive two parameters, one will be the id_prop and the other the id_cap.

1

This is happening because you are getting two id parameters on your route, and then it is only getting the first one

href=http://h20.laravel/cliente/prop/1/cap/1 

What version of the Laravel?

Depending on the version you can use request()->getParameter('id_cap') to get the data of this parameter only, which is coming on the route

Browser other questions tagged

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