Search code examples
phplaravelgetrequestlaravel-5.3

How to get parameter in laravel 5.3?


My view is like this :

<li class="{{ Request::is('users*') ? 'active' : '' }}">
    <a href="{!! route('users.index', 2016) !!}">
        <i class="fa fa-circle-o"></i> YEAR 2016
    </a>
</li>
<li>
    <a href="{!! route('users.index', 2017) !!}">
        <i class="fa fa-circle-o"></i> YEAR 2017
    </a>
</li>

When I hover over the link to the year 2016, the url will be like this :

localhost/mysystem/public/users?2016

My routes\web.php is like this :

Route::get('users/index?{year}', 'UserController');

Route::resource('users', 'UserController');

And my controller user is like this :

public function index(Request $request)
{
    $year = $request->input('year');
    echo $year;die()
}

There is exist error like this :

UnexpectedValueException in Route.php line 646: Invalid route action: [App\Http\Controllers\UserController]

Is there any people who can help me?


Solution

  • Your route should be as:

    Route::get('users/index/{year}', 'UserController@index')->name('users.index.year');
    

    Your controller as:

    public function index(Request $request, $year)
    {
        echo $year;die()
    }
    

    Then you can use it in your view as:

    {{ route('users.index.year', ['year' => 2016]) }}