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 :
- How to Send Email with Laravel 10
- Laravel 9 REST API with Passport Authentication Tutorial
- How To Secure Laravel Website From Hackers
- How To Clone Laravel
- Laravel One to Many Eloquent Relationship Tutorial With Example
- Laravel Tap Payment Gateway Integration Tutorial Example
- Livewire Create Multiple Select2 Dynamic Dropdown In Laravel 9 8 Example
- Laravel Insert Data Using Core sql query
- Import Export Database in JSON Format Laravel
- Email Send Using Queue In Laravel