Doubt with route

0

I've been trying to make a route for a while, 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 have tried it anyway, but I have not yet understood the logic in the system ...

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

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

    
asked by anonymous 11.09.2018 / 21:25

1 answer

1

I'll give you two options, choose the one that's convenient.

First choice

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

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

And in your Controller, you would drop the parameter $id and get the value of id by request , getting:

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

Finally, in your view, you would remove from the function route in action its second parameter, being just:

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

Function documentation query you find here .

Second option

You would have to use Javascript to get the selected value of <select/> , it would look 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>

If you'd like to learn more about the code above, I relied on that here and in . I also suggest taking a look at Route Model Binding , which would further simplify your code if you used with the above solution.

Ps: I believe the above JQuery solution needed too.

    
13.09.2018 / 01:17