In this post, you will learn the process to get the browser name and version in Laravel, with a step-by-step guide. You will understand how to get the browser information in Laravel through a comprehensive explanation of how to get the browser name and version. We will examine an example using the Laravel jenssegers/agent package. So, let’s explore the details.
You must have a Laravel 6 or higher version to use or check this example.
To get the browser name and version in laravel application, we will use jenssegers/agent composer package. jenssegers/agent will help to get the browser name, browser version, device name, and platform name, check if a device is mobile, desktop, or tablet, this will also detect if the user is a robot or not.
Let’s do this by following the steps-
Step 1: Install the jenssegers/agent Package
In this step, you need to run the following command to install the package:
composer require jenssegers/agent
Step 2: Laravel Get Browser Name and Version
Write the code in the following path – 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: Browser name and Browser version.
Step 3: Laravel Get Device Name
Write the code in the following path – 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: Device name or type.
Step 4: Laravel Get Platform Name
Write the code in the following path – 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: Windows / Linux.
Step 5: Laravel Check Device Type
Write the code in the following path – 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 Tablet.'; }
else if (Agent::isPhone() ) {
$result = 'Yes, This is Phone.'; }
dd($platform);
}
}
Output: Yes, This is Desktop
Step 6: Laravel Check User is Robot or not.
Write the code in the following path – 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( 'Yes, User is Real.');
}
}
Output: User is Real/ Robot
We hope, this might be helpful for you.