ItSolutionStuff.com

How to Detect Devices is Mobile or Desktop in Laravel?

By Hardik Savani • April 16, 2024
Laravel

Few days ago i need to detect devices access from mobile, desktop or tablet in laravel 5 app. i find out jessenger agent package for detection of mobile or desktop in laravel 5.8. we can easily check which devices use user like mobile, 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, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 project.

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...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Show Data in Modal using Ajax in Laravel?

Read Now →

How to Use and Install Font Awesome Icons in Laravel?

Read Now →

How to Install JQuery in Laravel Vite?

Read Now →

How to Read Content from PDF File in Laravel?

Read Now →

How to Remove Public from URL in Laravel 10?

Read Now →

How to Install Bootstrap 5 in Laravel 10?

Read Now →

Laravel Carbon Change Timezone Example

Read Now →

Laravel Array Length Validation Example

Read Now →

How to Check Query Execution Time in Laravel?

Read Now →

Laravel Password and Confirm Password Validation Example

Read Now →

How to Increment and Decrement a Column Value in Laravel?

Read Now →

How to Send Mail using Gmail SMTP in Laravel?

Read Now →

How to Get Query Log in Laravel Eloquent?

Read Now →

Laravel Create JSON File & Download From Text Example

Read Now →