In this artical, i will let you know how to detect mobile or desktop browser in laravel 5 application. we will use jenssegers/agent package for detect device. we can easily detect device mobile or tablet or desktop.
jessenger ajent plugin provide method to get all user agent values with their pre define function by package. they provide function like isMobile(), isTablet(), isDesktop() and device(). there are more helper provide for user agent. we can easily use with laravel 5.1, laravel 5.2, laravel 5.3, laravel 5.4, laravel 5.5, laravel 5.6, laravel 5.7 etc.
You have to just follow bellow tutorial to detect mobile or desktop access device.
Install jessenger/ajent Package
We need to install jessenger/ajent composer package for getting user ajent value, so you can install using following command:
composer require jenssegers/agent
After that you need to set providers and alias.
config/app.php
.....
'providers' => [
....
Jenssegers\Agent\AgentServiceProvider::class,
]
'aliases' => [
....
'Agent' => Jenssegers\Agent\Facades\Agent::class,
]
.....
Now bellow i will create route and you can see how to use it.
Detect Is Mobile:
Route::get('detect', function()
{
$agent = new \Jenssegers\Agent\Agent;
$result = $agent->isMobile();
dd($result);
});
Detect Is Desktop:
Route::get('detect', function()
{
$agent = new \Jenssegers\Agent\Agent;
$result = $agent->isDesktop();
dd($result);
});
Detect Is Tablet:
Route::get('detect', function()
{
$agent = new \Jenssegers\Agent\Agent;
$result = $agent->isTablet();
dd($result);
});
You can also make condition in view blade file like as bellow:
@if((new \Jenssegers\Agent\Agent())->isDesktop())
<link rel="stylesheet" href="{{ asset('front/css/desktop.css') }}" />
@endif
@if((new \Jenssegers\Agent\Agent())->isMobile())
<link rel="stylesheet" href="{{ asset('front/css/mobile.css') }}" />
@endif
I hope you found your best tutorial...
Do you like below Tutorials ?
- How to Display Data in Angular 10?
- Angular 10 Routing Module Example Tutorial
- Angular 10 CRUD Operations with Web API Example
- Laravel 8 CRUD Operation Step By Step Tutorial
- Solved - Target class [ProductController] does not exist in Laravel 8
- Step by Step Form Validation in Laravel 8 Example
- Laravel 8 Image Upload with Preview Example
- Laravel 8 Multiple Images Upload with Preview Example