Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jrdavidson's avatar

Sending String Parameter From Route to View

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
0 likes
9 replies
patrickadvance's avatar

Do this

public function index($state = 'active')
{
    $users = User::where('state',$state)->get();

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

jrdavidson's avatar

@patickadvance You are taking away the state getting put through the String title helper though. So that doesn't answer the question.

jrdavidson's avatar

There may be some misunderstanding. Is there a way I can with a view composer or something to where on any index page it will show the state as capitalized when the variable is being requested.

jrdavidson's avatar

@neeonline Doesn't your first suggestion add logic to the view. I was starting to think about a view composer but not sure if that is the right direction.

neeonline's avatar

@XTREMER360 - This transformation that you want needs to happen somewhere.

In your first question you don't want to add it in all controller's methods, so that means it need to be done for all routes that uses it. The only way will be doing it using my second suggestion.

In my opinion this is a cosmetic change and should belong into the view.

siangboon's avatar

yes, view composer seem the better option for your need.

Please or to participate in this conversation.