Doubt with route

Asked

Viewed 35 times

0

I’ve been trying for a while to make a route work, but so far nothing...

Controller:

public function profissionais(Request $request, $id){
    $vinculo = session()->get('vinculo');

    $profissionais = Vinculo::where('unidade_id', '=', $id)->get();

    $unidades = Unidade::where('municipio_id', '=', $vinculo->usuario->municipio_id)
                        ->get();

    $profissionais = $id;

    return view('relatorios.profissionais', compact('unidades', 'profissionais'));
}

Form:

<form method="GET" action="{{route('relatorios.profissionais', 'id')}}">
    <select class="js-example-basic-single" name="id" required>
        @foreach($unidades as $unidade)
            <option value="{{$unidade->id}}">{{$unidade->descricao}}</option>
        @endforeach
    </select>
    <span class="input-group-btn">
        <button class="btn btn-primary" type="submit">Listar</button>
    </span>
</form>

web php.:

Route::get('/relatorios/profissionais/{id}', 'RelatorioController@profissionais')->name('relatorios.profissionais');

I’ve tried it anyway, but I see I still don’t understand the logic in the system...

What I want is for the url to appear like this: /relatorios/profissionais/4 and the controller receives the number 4 in the variable $id and seek the links.

But at the moment the url is being shown like this: relatorios/profissionais/id?id=4

  • The second parameter of the route method that is in the attr action of the form must be an id (numeral). 4 or '4' {route('reports.professionals', 4)}} But you still can’t understand... I think I needed to see your javascript for this. Maybe put the route inside the foreach, in the attr value of each option... Then in the select change event you exchange the form attributo action. <option value="{{route('reports.professionals', $unit->id)}}">

1 answer

1


I’ll give you two choices, whichever is convenient.

First option

Instead of passing the value on the route like this {id}, you can use the query strings. Then on your route, you would track the {id} thus:

Route::get('/relatorios/profissionais', 'RelatorioController@profissionais')->name('relatorios.profissionais');

And in your Controller, you would delete the parameter $id and take the value of id for request, getting:

public function profissionais(Request $request){
    $id = $request->query('id');

Finally, in your view, you would delete from the function route in the action its second parameter, leaving only:

<form method="GET" action="{{route('relatorios.profissionais')}}">

Job documentation query you find here.


Second option

You would have to use Javascript to get the selected value from <select/>, would be more or less something like this:

// Seu action não estaria mais definido aqui.
<form id="form_unidade" method="GET" onclick="submitForm()">
    <select id="select_unidade" class="js-example-basic-single" name="id" required>
...

// Mas seria atribuido o action aqui.
<script type="text/javascript">
    function submitForm(){
    var e = document.getElementById("select_unidade");
    var unidade_id = e.options[e.selectedIndex].value;

    $("#form_unidade").attr("action", "www.seusite.com/relatorios/profissionais/" + unidade_id);
    $("#form_unidade").submit();
 }
 </script>

In case you want to know more about the code above, I based myself on this here and in that. I suggest also take a look at Route Model Binding, which would further simplify your code if you used the above solution.

Ps: I believe the above solution needs Jquery too.

Browser other questions tagged

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