A package that can detect mobile or desktop user in Laravel

Still troubled with how to detect mobile device or desktop device?

Patrick Wan
3 min readApr 26, 2022

--

Sometimes we might think about how do we control our web view in mobile or tablet. Today I gonna should you a package that can let us easy to manage it, it will help us more it our development if you are doing desktop and mobile view.

The package I gonna introduce is jenssegers/agent. A PHP desktop/mobile user agent parser with support for Laravel, based on Mobile Detect with desktop support and additional functionality.

This package can install it via composer with a simple command:

composer require jenssegers/agent

After the installation you might have an option in Laravel which you can add the service provider in your config or not. If yes, please follow the following below:

Add the Service Provider in config/app.php:

Jenssegers\Agent\AgentServiceProvider::class,

And add the Agent Alias to config/app.php:

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

Basic Usage

Let start with the basic, when we want to use the agent, we must include the provider:

use Jenssegers\Agent\Agent;

$agent = new Agent();

There was a few function we may use it in our development which is

$agent->is('Windows');
$agent->is('Firefox');
$agent->is('iPhone');
$agent->is('OS X');

The example above is to check are your currently browser platform or device.

There have another way which is the example below is also a easy way to detect the mobile device which are using IOS or Android.

$agent->isAndroidOS();
$agent->isNexus();
$agent->isSafari();

Next, check for mobile device or desktop device:

$agent->isMobile();
$agent->isTablet();
$agent->isDesktop();
$agent->isPhone(); // this is to…

--

--

Patrick Wan

My name’s Patrick. I’m a Software Developer, experience in Laravel, Vue Js, React Js, Livewire, Jquery, Codegniter, NPM, GIT. I have 5 years plus experience.