Home > Mobile >  How to check a url contains particular word in laravel blade file
How to check a url contains particular word in laravel blade file

Time:04-11

I want to check a particular word example "myproject" in the URL, can check it in the blade file.

CodePudding user response:

You can like this:

 use Illuminate\Support\Str;
 $url = url()->full();
 $contains = Str::contains($url, 'myproject');
 // or check two or more word
 $contains = Str::contains($myString, ['myproject', 'secondword']);
 if($contains){
  echo "myproject exists in URL";
 }else{
  echo "myproject is not exists in URL"
 }

CodePudding user response:

A good practice is to name the routes. Then you can check easily with

if (\Route::is('myproject')) {
   ... route name is myproject
}

// or

if (\Route::current()->getName() === 'myproject') {
   ... route name is myproject
}

If you dont name the route you can search in the url string. The url string you can get with:

  1. $request->url()

  2. url()->current()

  3. url()->full();

Then you can check like:

if (str_contains(url()->current(, 'myproject')) {
   // ... url  contains myproject
}
  • Related