Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Route pass additional variables

I have a user controller which returns users list, each user belongs to different group.

ex:- $groups = ['student_users' => 1, 'teacher_users' => 2, .....]

I can create routes such as below to access them

Route::get('users/{id}', [
        'as'            =>  'user',
        'uses'          =>  'Admin\UserController@listUser'
    ]);

But i want to create more user friendly or seo friendly say like this

Route::get('users/student', [
        'as'            =>  'student',
        'uses'          =>  'Admin\UserController@listUser'
    ]);

Route::get('users/teacher', [
        'as'            =>  'teacher',
        'uses'          =>  'Admin\UserController@listUser'
    ]);
Route::get('users', [
        'as'            =>  'student',
        'uses'          =>  'Admin\UserController@listUser'
    ]);//by default shows students list.

And i want to pass the id via route not via url. Whats the best way to do it.

like image 269
Abhishek Salian Avatar asked Jan 09 '23 01:01

Abhishek Salian


1 Answers

do as following

Route::get('users/student', [
    'as'            =>  'student',
    'type'          =>  'student',
    'uses'          =>  'Admin\UserController@listUser'
]);

in controller you can get type as below

public function listUser(\Illuminate\Http\Request $request)

   $action = $request->route()->getAction();
   dd($action['type']); 
}

type is just an example. You can pass any variable.

I hope it helps.

like image 166
pinkal vansia Avatar answered Jan 16 '23 18:01

pinkal vansia