Do this
public function index($state = 'active')
{
$users = User::where('state',$state)->get();
return view('users.index', compact('users', 'state'));
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying to think of any better way to send through a string that has retrieved from a route parameter and has been run through a title string helper and echoed out into the view. The reason being is a have many other modules that share this same functionality and would like to make it easier to manage across all instances.
So I'm trying to figure out what would be the best strategy so that I don't have to continue adding the Str::title($state) and pass it to the view on every module that uses it. Is there a more strategic way of moving that logic somewhere.
Route::get('/users/state/{state?}', 'UsersController@index')->name('users.index');
Route::get('/students/state/{state?}', 'StudentsController@index')->name('teachers.index');
Route::get('/teachers/state/{state?}', 'TeachersController@index')->name('teachers.index');
UsersController
public function index($state = 'active')
{
$users = User::hasState($state)->get();
$state = Str::title($state);
return response()->view('users.index', compact('users', 'state'));
}
StudentsController
public function index($state = 'active')
{
$students = Student::hasState($state)->get();
$state = Str::title($state);
return response()->view('students.index', compact('students', 'state'));
}
TeachersController
public function index($state = 'active')
{
$teachers = Teacher::hasState($state)->get();
$state = Str::title($state);
return response()->view('teachers.index', compact('teachers', 'state'));
}
Users/index.blade.phjp
{{ $state }} Users
Teachers/index.blade.phjp
{{ $state }} Teachers
Students/index.blade.phjp
{{ $state }} Students
Please or to participate in this conversation.