Controller method getting wrong value in Laravel

0

A piece of my application consists of the client login and from there to register some pertinent information daily. When logging in, it falls on the screen to choose in which capture it will register the data. In the next one 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 for some capture, I'm always directed to the capture system page belonging to capture 1. Regardless of the capture that I choose, I'm always directed to the capture system 1. Using the method I am trying to find a way to do this, but I can not figure out how to do this.

 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 on my blade where I create the above links:

<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>
    
asked by anonymous 20.06.2018 / 17:05

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 laravel website

  

Route parameters are injected into route callbacks / controllers based   on their order - the names of the callback / controller arguments of the   not matter.    link

To solve your problem, your clienteCaptacao method should get two parameters, one being id_prop and the other id_cap .

    
20.06.2018 / 17:25
1

This is happening because you are getting two id parameters in your route, and there it is catching only the first

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

What version of laravel?

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

    
23.06.2018 / 19:08