jeudi 17 août 2017

Laravel 5.4 passing form select option value to a Route::get

I am trying to do the following within my Laravel 5.4 project.

In a users.index blade page I am listing all users from a database in a table. All the users belong to a district. Some districts have more users then the other.

At the top of this table view I created a form with a select field. The options hold the id's for the 10 districts:

<form class="form-horizontal" method="GET" action="/users/district">

            <div class="form-group col-md-5">
                <select name="district" class="form-control">
                    <option value="1">Amsterdam</option>
                    <option value="2">Arnhem</option>
                    <option value="3">Assen</option>
                    <option value="4">Groningen</option>
                    <option value="5">Leeuwarden</option>
                    <option value="6">Rotterdam</option>
                    <option value="7">Sittard</option>
                    <option value="8">Tilburg</option>
                    <option value="9">Utrecht</option>
                    <option value="10">Antillen</option>
                </select>
            </div>

            <div class="form-group col-md-4 col-md-offset-1">
                <button class="btn btn-md btn-primary">Haal gegevens op</button>
            </div>

            </form>

When I put the URL "http://localhost/users/district/5" in manually in the browser it shows me a table view with only users from the district with the ID of 5.

This is my UsersController:

public function perDistrict($district)
{
    $users = User::where('district_id', $district)
    ->paginate(12);

    $totalusers = User::count();

    return view('users.index', compact('users', 'totalusers'));
    }

I would like to use the form to get me the results but when I submit the form it show me this URL: "http://localhost/users/district?district=5" and it gives me no result of course.

How can I convert that to the URL "http://localhost/users/district/5" ?

Hope someone can help me.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2x8PpId
via IFTTT

Aucun commentaire:

Enregistrer un commentaire