Search code examples
phplaravellaravel-5.3

Why the result looks strange when I send different parameters to the same function? (laravel 5.3)


My view is like this :

...
<a class="btn btn-primary pull-right" style="margin-top: -10px;margin-bottom: 5px" href="{!! route('users.create.year', [$year]) !!}">
    Add New
</a>
...

...
@foreach($testArray as $key)
...
<a class="btn btn-primary btn-xs" href="{!! route('users.create.year', [$key['display'], $year]) !!}">
    <i class="glyphicon glyphicon-plus"></i>
</a>
...
@endforeach
...

My routes is like this :

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

My controller is like this :

public function create($year, $display = null)
{
    echo $display.' : display <br>';
    echo $param_thang. ' : year';die();
    ...
}

When I call url like this : http://localhost/mysystem/public/users/create/2016, it works.

There result is like this :

: display
2016 : year

When I call url like this : http://localhost/mysystem/public/users/create/14144499452111901/2016 The result is like this :

2016 : display
14144499452111901 : year

It looks strange, the result is reversed

Why did it happen?


Solution

  • It's because your route is:

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

    So the first parameter is always going to be the year, and the 2nd parameter is always going to be the display variable.