How do I search within the routes if it contains a specific word within PHP Laravel

Issue

How do I search within the routes if it contains a specific word within PHP Laravel
I want to find the word (students) within the title of the current page so that I can put a synonym or translation
my code…

if ( Route::currentRouteName() …. what to write here …. ‘students’ )

I have a code that will be executed here in case the word (students) is found.

endif

I have these links

students/index
students/create
students/edit

I want to search for the word (students) if it is in the link

Solution

Here is another solution which seems to me much more efficient since it uses the built-in methods of Laravel.

In your example, if you want to check the name of your route, you can do so using the Laravel helpers:

use Illuminate\Support\Str;

if(Str::is('students/*', Route::current()->getName())) {
  //
}

Doc: https://laravel.com/docs/9.x/helpers#method-str-is

Now, to give an alternative to the solution of @Manuel Glez, you can directly inspect the path/route of the request using a pattern:

if ($request->is('students/*')) {
  //
}

Doc: https://laravel.com/docs/9.x/requests#inspecting-the-request-path

Answered By – Jedupont

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published