Laravel Detect Device is Mobile or Desktop
Hi, we can easily detect any device in Laravel by using a package. Just follow these simple steps:
Table of Contents
Step 1 : Install Package
We need to install jessenger/ajent package by composer. Go to Laravel project folder and run this command:
composer require jenssegers/agent
Step 2 : Config app.php
Now we need to set provider and alias. Navigate to config/app.php and add Jenssegers classes to providers and aliases like this:
'providers' => [
....
Jenssegers\Agent\AgentServiceProvider::class,
]
'aliases' => [
....
'Agent' => Jenssegers\Agent\Facades\Agent::class,
]
Step 3 : Create Routes
Let’s create routes to detect devices. Open routes/web.php and create routes like these:
Detect Mobile:
Route::get('detect', function () {
$agent = new \Jenssegers\Agent\Agent;
$result = $agent->isMobile();
if ($result)
return "Yes, This is Mobile.";
else
return "No, This is not Mobile.";
});
Detect Desktop:
Route::get('detect', function () {
$agent = new \Jenssegers\Agent\Agent;
$result = $agent->isDesktop();
if ($result)
return "Yes, This is Desktop.";
else
return "No, This is not Desktop.";
});
Detect Phone:
Route::get('detect', function () {
$agent = new \Jenssegers\Agent\Agent;
$result = $agent->isPhone();
if ($result)
return "Yes, This is Phone.";
else
return "No, This is not Phone.";
});
Detect Tablet:
Route::get('detect', function () {
$agent = new \Jenssegers\Agent\Agent;
$result = $agent->isTablet();
if ($result)
return "Yes, This is Tablet.";
else
return "No, This is not Tablet.";
});
Detect Robot:
Route::get('detect', function () {
$agent = new \Jenssegers\Agent\Agent;
$result = $agent->isRobot();
if ($result)
return "Yes, This is Robot.";
else
return "No, This is not Robot.";
});
Step 4 : Use in Blade File
@if((new \Jenssegers\Agent\Agent())->isDesktop())
{{-- your code --}}
@endif
@if((new \Jenssegers\Agent\Agent())->isMobile())
{{-- your code --}}
@endif
Thank you. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)