How To Detect Device is Mobile or Desktop in Laravel 8

Share this Article
Reading Time: 4 minutes
769 Views

Inside this article we will see the concept of device detection composer package inside laravel application. How to detect device is mobile or desktop in Laravel 8, we will discuss in this article.

Always developer or designer wants to know that whenever they open application which device is being to used for it. So for this we will use jessenger agent package for device detection.

Device detection in application is process in which you will the device information, browser information etc where your application runs.

Learn More –

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


Install jessenger/ajent Package

Open project into terminal and run this command to install this device detection package.

$ composer require jenssegers/agent

It will install jessenger/ajent package inside applcation.


Provider & Alias Settings

Open app.php from /config folder.

Search for providers & aliases and these lines into it.

.....

'providers' => [
 ....

 Jenssegers\Agent\AgentServiceProvider::class,

],

'aliases' => [
 ....

 'Agent' => Jenssegers\Agent\Facades\Agent::class,

]

.....

Usage of Detection Package

Before use of jenssegers/agent package, we need to create an instance. By using that instance we can call device detection methods.

Object/Instance Creation

$agent = new \Jenssegers\Agent\Agent;

Detect Mobile

$agent->isMobile();

Detect Laptop/Desktop

$agent->isDesktop();

Detect Tablet

$agent->isTablet();

All these methods returns boolean value.

Here, we have a sample code for application to check device.

//...

Route::get('detect-device', function () {

    // object
    $agent = new \Jenssegers\Agent\Agent;

    // laptop/desktop
    $result1 = $agent->isDesktop();

    // mobile
    $result2 = $agent->isMobile();

    // tablet
    $result3 = $agent->isTablet();

    // returns boolean value of each variable
    echo $result1." , ".$result2. " , ".$result3; 
});

//...

How to Use Agent Methods in Blade Layouts

Also we have an option available when we need to use $agent methods in blade layouts directly.

#Desktop/Laptop
@if((new \Jenssegers\Agent\Agent())->isDesktop())
      DO STUFF
@endif

#Mobile
@if((new \Jenssegers\Agent\Agent())->isMobile())
      DO STUFF
@endif

#Tablet
@if((new \Jenssegers\Agent\Agent())->isTablet())
      DO STUFF
@endif

More Useful Notes About Package

We have lots of methods available by this Agent facade. Using these methods you can know platform, browser, browser version, etc.

$agent->platform();
// Ubuntu, Windows, OS X, ...

$agent->browser();
// Chrome, IE, Safari, Firefox, ...

$browser = $agent->browser();

$version = $agent->version($browser);

$platform = $agent->platform();

$version = $agent->version($platform);

$agent->is('Windows');

$agent->is('Firefox');

$agent->is('iPhone');

$agent->is('OS X');

$agent->isAndroidOS();

$agent->isNexus();

$agent->isSafari();

$agent->languages();
// ['nl-nl', 'nl', 'en-us', 'en']

$agent->device();
// iPhone, Nexus, AsusTablet, ...

$agent->isDesktop();

$agent->isPhone();

$agent->isMobile();

$agent->isTablet();

$agent->isRobot();

$agent->robot();
// robot name

We hope this article helped you to learn How To Detect Device is mobile or desktop in Laravel 8 Tutorial in a very detailed way.

Buy Me a Coffee

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here