'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.
Solution 1:[1]
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"
}
Solution 2:[2]
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:
$request->url()url()->current()url()->full();
Then you can check like:
if (str_contains(url()->current(, 'myproject')) {
// ... url contains myproject
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Akash Bhalani |
| Solution 2 | Maik Lowrey |
