The is() method will check retrive method path from the url and check with whatever you pass string inside this method. The is() is used to collect the current URL from the request and it will match with particular pattern which we specify in this method argument.
Sometimes developers need to select the current page category for the user to know what they are.
Example 1:
Simple request check, If the request comes without a path then it will return true else false.
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { \Request::is('/'); // OUTPUT => true /*-----------------------------------------*/ \Request::is('home'); // OUTPUT => false });
Example 2:
If the request comes with a home path URL then it will give us a true request else false.
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/home',function(){ \Request::is('home'); // OUTPUT => true \Request::is('contact'); // OUTPUT => false });
Example 3:
a the tag inside the class, Whats I what that whenever a request comes I want that category should hover or selected.
So I added a condition inside the tag class if the request returns true then it will add a class active so it can be selected. Users know where they are on current category page.
<a href="https://www.webappfix.com" class="{{ \Request::is('/') ? 'active' : '' }}">Home</a> <a href="https://www.webappfix.com" class="{{ \Request::is('contact') ? 'active' : '' }}">Contact</a> <a href="https://www.webappfix.com" class="{{ \Request::is('category') ? 'active' : '' }}">Category</a>
Example 4:
Request checked in blade file if condition if true the perform something action or else will ready.
@if(\Request::is('/','home','contact')) @else
Example 5:
Request check with URL parameters. For example users id, slug, etc...
So using * beside the path can check.
Request check with an id parameter. If the request will be home/{id} then it will return true else false.
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/home/{id}', function () { \Request::is('home/*'); // OUTPUT => true });
Thanks For Reading Our Post.
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Median() Method | How It is Different From avg() Method In Laravel Collection
- Laravel 9 Dropzone Multiple File Upload Example
- How to return laravel collection in json format - toJson() Method - Laravel Collection
- Laravel Adding Social Media Share buttons Advanced Tutorial Example
- How To Secure Laravel Website From Hackers
- All Know About Different Root Path Of The Laravel Application With Example
- Laravel 9 File manager integration tutorial
- How To Add Google reCAPTCHA v2 In HTML Form Based PHP/Laravel Website
- Laravel 10 PDF Export Example With DomPDF
- Laravel 9 Google Drive File Upload/Store Tutorial.