Athsal KJ
Athsal KJ

Reputation: 29

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

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

Upvotes: 3

Views: 1518

Answers (2)

Maik Lowrey
Maik Lowrey

Reputation: 17566

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
}

Upvotes: 1

Akash Bhalani
Akash Bhalani

Reputation: 192

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"
 }

Upvotes: 0

Related Questions