How to Get Browser Info in Laravel Example

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this, I will explain with you how we can get the browser information in our laravel application. Here we will see how to get browser name, version name. And also we will show a simple example of get browser information.

For this here we will use jenssegers package for get browser information. Now for all the information so we need to know about the user all accessible by using Agent (jenssegers/agent) package. So Agent makes Laravel equipped to detect the user environment. Here I will detect user browser and also its version by using the jenssegers package.

Let’s start and follow the bellow example step by step:

Install jenssegers/agent Package

At first here in this step need to install the jenssegers/agent Package. By using bellow command we can install that. So fire this command

composer require jenssegers/agent
Add providers and aliases

So after the install package need to add providers and aliases in the “config/app.php” file.

following path: config/app.php

'providers' => [
	....
	Jenssegers\Agent\AgentServiceProvider::class,
],
'aliases' => [
	....
	'Agent' => Jenssegers\Agent\Facades\Agent::class,
]

Then after the following method to get browser information.

Get Browser Name

Now here the controller for get user browser name of a Chrome, IE, Safari, Firefox,etc

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $agent = new Agent();
    $browser = $agent->browser();
    dd($browser);
}

Output

"Chrome"
Get Browser version

Now the controller for get user browser version.

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $agent = new Agent();
    $browser = $agent->browser();
    $version = $agent->version($browser);
    dd($version);
}

Output

"79.0.3945.130"
Get User language

So now this controller for get user browser language of ‘nl-nl’, ‘nl’, ‘en-us’, ‘en’,etc.

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $agent = new Agent();
    $languages = $agent->languages();
    dd($languages);
}

Output

array:4 [
  0 => "en-us"
  1 => "en"
  2 => "la"
  3 => "gu"
]
Get Platform

So now this controller for get user platform of a Ubuntu, Windows, OS X,etc

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $agent = new Agent();
    $platform = $agent->platform();
    dd($platform);
}

Output

"Windows"
Get Platform Version

here the controller for get user Platform version,

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $agent = new Agent();
    $platform = $agent->platform();
    $version = $agent->version($platform);
    dd($version);
}

Output

"10.0"

Read Also : Laravel Pagination with Ajax Example

Thank you read.Hope it will help you. Also follow on facebook

About code chef

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 code chef →