How to Get Browser Name and Version in Laravel?

By Hardik Savani September 9, 2022 Category : Laravel

Hi Dev,

I will explain step by step tutorial how to get browser name and version in laravel. you will learn laravel get browser info. step by step explain laravel get browser name and version. We will look at an example of laravel jenssegers/agent example. So, let us dive into the details.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.

We will use jenssegers/agent composer package to get browser name and version in laravel application. jenssegers/agent will help to get browser name, browser version, device name, and platform name, check if a device is mobile, desktop, or tablet, and check whether the user is a robot or not.

Without any further ado, let's see one by one example:

Install jenssegers/agent Package

In this step, we will need to install jenssegers/agent composer package to getting request info. let's run the below command to install.

composer require jenssegers/agent

Example 1: Laravel Get Browser Name and Version

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Facades\Agent;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$browser = Agent::browser();

$version = Agent::version($browser);

dd($browser, $version);

}

}

Output:

Chrome

105.0.0.0

Example 2: Laravel Get Device Name

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Facades\Agent;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$device = Agent::device();

dd($device);

}

}

Output:

WebKit

Example 3: Laravel Get Platform Name

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Facades\Agent;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$platform = Agent::platform();

dd($platform);

}

}

Output:

Linux

Example 4: Laravel Check Device is Desktop, Tablet or Phone

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Facades\Agent;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

if (Agent::isMobile()) {

$result = 'Yes, This is Mobile.';

}else if (Agent::isDesktop()) {

$result = 'Yes, This is Desktop.';

}else if (Agent::isTablet()) {

$result = 'Yes, This is Desktop.';

}else if (Agent::isPhone()) {

$result = 'Yes, This is Phone.';

}

dd($result);

}

}

Output:

Yes, This is Desktop.

Example 5: Laravel User is Robot or Not

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Facades\Agent;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

if (Agent::isRobot()) {

dd('Yes, User is Robot.');

}

dd("User is real!");

}

}

Output:

Yes, User is Robot.

I hope it can help you...

Tags :