Throughout this Laravel 11 Detect Mobile or Desktop example we are going to share How to detect devices that is mobile or desktop or laptop in Laravel 11.
Detecting mobile, desktop, laptop phone, etc is required sometimes in Laravel, So we use jenssegers/agent package to easily detect any device.
So, follow the following step-by-step guide to detecting mobile or desktop devices in the Laravel application.
#1 Download Laravel App
First, install a new Laravel application using the following command. This command will generate a new project named as “laravel-detect-device” app in your machine.
composer create-project --prefer-dist laravel/laravel laravel-detect-device
Next, Go into your application directory:
cd laravel-detect-device
#2 Setup Database Credentials
Now open the .env file and add your database credentials such as database name, username and password.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=
#3 Install & Configure jenssegers Package
Next, you need to install jessenger/ajent package by the composer using the following command:
composer require jenssegers/agent
Now we need to set the provider and alias. Navigate to config/app.php and add Jenssegers classes to providers and aliases like this:
config/app.php
'providers' => [ .... Jenssegers\Agent\AgentServiceProvider::class, ] 'aliases' => [ .... 'Agent' => Jenssegers\Agent\Facades\Agent::class, ]
#4 Create Routes for Detect Devices
Now, go to the routes folder and open routes\web.php file, add the following routes to the file for detecting devices in the Laravel application:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PageController;
Route::get('detect-device', [PageController::class, 'detuctDebice'])->name('detect-device');
#5 Create Controller
Now you need to create a new controller for running the following command;
php artisan make:controller PageController
Now, open app/http/controllers/PageController.php file and update the below code on it.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Jenssegers\Agent\Agent; class PageController extends Controller { public function detuctDebice() { $agent = new Agent; $mobileResult = $agent->isMobile(); if ($mobileResult) { $result = 'Yes, This is Mobile.'; } $desktopResult= $agent->isDesktop(); if ($desktopResult) { $result = 'Yes, This is Desktop.'; } $tabletResult= $agent->isTablet(); if ($tabletResult) { $result = 'Yes, This is Desktop.'; } $tabletResult= $agent->isPhone(); if ($tabletResult) { $result = 'Yes, This is Phone.'; } dd($result); } }
#6 Detect Device Use in Blade File
You can use the device detector package easily in laravel blade file just like below.
@if((new \Jenssegers\Agent\Agent())->isDesktop())
{{-- your code --}}
@endif
@if((new \Jenssegers\Agent\Agent())->isMobile())
{{-- your code --}}
@endif
Conclusion
I hope you enjoy this. These Laravel versions are supported for detecting device mobile or desktop:
- Laravel 8
- Laravel 9
- Laravel 10
- Laravel 11