How to Detect Mobile or Desktop by Using jenssegers/agent in Laravel?

Today now in this artical i will show you How to Detect Mobile or Desktop by Using jenssegers/agent in Laravel ?Here i will talk about laravel Agent. Here i will let you tech about how to detect mobile or desktop browser in laravel application by using laravel agent. Here i will use jenssegers/agent package for detect the device. we can easily detect any device mobile or tablet or desktop by using laravel agent package.

jessenger agent plugin is provide the method to get all the user agent values with their pre define function by using package. It is provide function as like isMobile(), isTablet(), isDesktop() and device().

Install jessenger/agent Package

Just We need to install the jessenger/agent package by using composer. Go to the Laravel project folder and run this command:

composer require jenssegers/agent

After that we need to set the providers and alias.

config/app.php
.....
'providers' => [
	....
	Jenssegers\Agent\AgentServiceProvider::class,
]
'aliases' => [
	....
	'Agent' => Jenssegers\Agent\Facades\Agent::class,
]
.....
Create Routes

Now Let’s create the routes to detect devices.

routes/web.php

Detect Mobile:
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

Read Also: How we can create virtual host in ubuntu apache?

close

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →