How to get the current URL in Laravel's @if statement?

PHPz
Release: 2023-08-23 14:14:02
forward
963 people have browsed it

How to get the current URL in Laravels @if statement?

To get the current URL you can use the method explained in the example below −

Example 1

<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; use IlluminateHttpResponse; class UserController extends Controller { public function index(Request $request) { return view('test'); } } 
Copy after login

test.blade.php is −

<!DOCTYPE html> <html> <head> <style> body {
         font-family: 'Nunito', sans-serif;
      } </style> </head> <body class="antialiased"> <div> @if (Request::path() == 'users') <h1>The path is users</h1> @endif </div> </body> </html> 
Copy after login

In the test.blade.php file, use Request::path() to check if it points to the user and then only display the h1 tag. Request::path() Returns the URL currently in use.

Example 2

In this example, let's use the url()->current() method as shown in the example below. url()->current() Returns the full path of the current URL.

<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; class UserController extends Controller { public function index(Request $request) { return view('test'); } } 
Copy after login
Copy after login

Test.blade.php

<!DOCTYPE html> <html> <head> <style> body {
         font-family: 'Nunito', sans-serif;
      } </style> </head> <body class="antialiased"> <div> @if (url()->current() == 'http://localhost:8000/users') <h1>The path is users</h1> @endif </div> </body> </html> 
Copy after login

When executing the above example, it will print the following on the browser −

The path is users
Copy after login
Copy after login
Copy after login

Example 3

In this example, we will use Request::url(). Its output is the same as url()->current(), which returns the full URL as shown in the example below −

<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; class UserController extends Controller { public function index(Request $request) { return view('test'); } } 
Copy after login
Copy after login

Test.blade.php

<!DOCTYPE html> <html> <head> <style> body { font-family: 'Nunito', sans-serif; } </style> </head> <body class="antialiased"> <div> @if (Request::url() == 'http://localhost:8000/users') <h1>The path is users</h1> @endif </div> </body> </html> 
Copy after login

When executing the above example, it will print the following on the browser −

The path is users
Copy after login
Copy after login
Copy after login

Example 4

Use Request::is()

<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; class UserController extends Controller{ public function index(Request $request) { return view('test'); } } 
Copy after login

Test.blade.php

<!DOCTYPE html> <html> <head> <style> body { font-family: 'Nunito', sans-serif; } </style> </head> <body class="antialiased"> <div> @if (Request::is('users')) <h1>The path is users</h1> @endif </div> </body> </html> 
Copy after login

In the above example, Request::is() is used. It returns true/false indicating whether the given string exists in the URL.

When executing the above example, it will print the following on the browser −

The path is users
Copy after login
Copy after login
Copy after login


The above is the detailed content of How to get the current URL in Laravel's @if statement?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!