- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the Current URL inside the @if Statement in Laravel?
To get the current URL you can use the methods explained in the example below −
Example 1
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Illuminate\Http\Response; class UserController extends Controller { public function index(Request $request) { return view('test'); } }
The 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>
Inside the test.blade.php the Request::path() is used to check if it points to users and then only the h1 tag is displayed. The Request::path() returns the current URL being used.
Example 2
In this example let us make use of url()->current() method as shown in the example below. The url()->current() gives the current URL full path.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function index(Request $request) { return view('test'); } }
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>
On executing the above example, it prints the following on the browser −
The path is users
Example 3
In this example will make use of Request::url() . The output of it is same as url()->current() , it return the full URL as shown in the example below −
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function index(Request $request) { return view('test'); } }
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>
On executing the above example, it prints the following on the browser −
The path is users
Example 4
Using the Request::is()
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller{ public function index(Request $request) { return view('test'); } }
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>
In the example above Request::is() is used. It returns true/false if the given string is present in the URL.
On executing the above example, it prints the following on the browser −
The path is users
- Related Articles
- How to get the current URL with JavaScript?
- How to get current URL in JavaScript?
- How to get current URL in jQuery?
- How to handle python exception inside if statement?
- How to get an object containing parameters of current URL in JavaScript?
- How to get the server IP with Laravel?
- How to pass an array as a URL parameter in Laravel?
- How do I get current URL in Selenium Webdriver 2 Python?
- How to access images inside a public folder in Laravel?
- How to get the contents of the HTTP Request body in Laravel?
- How to get the last inserted ID using Laravel Eloquent?
- How to call a controller function inside a view in Laravel 5?
- How to get the current year in JavaScript?
- How to get the current date in Java?
- Can HTML be embedded inside PHP “if” statement?
