Hello Devs,
In this tutorial, we are going to learn how to detect mobile or desktop browser in laravel application.
Follow this step by step guide given below:
Install jessenger/ajent Package
composer require jenssegers/agent
config/app.php
.....
'providers' => [
....
Jenssegers\Agent\AgentServiceProvider::class,
]
'aliases' => [
....
'Agent' => Jenssegers\Agent\Facades\Agent::class,
]
.....
Create Routes
Detect Mobile: routes/web.php
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 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.";
});
Use in Blade File
@if((new \Jenssegers\Agent\Agent())->isDesktop())
{{-- your code --}}
@endif
@if((new \Jenssegers\Agent\Agent())->isMobile())
{{-- your code --}}
@endif
I hope this example helps you.