Sending String Parameter From Route to View

Posted 2 months ago by xtremer360

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?}', '[email protected]')->name('users.index');
Route::get('/students/state/{state?}', '[email protected]')->name('teachers.index');
Route::get('/teachers/state/{state?}', '[email protected]')->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 sign in or create an account to participate in this conversation.