How to Get Browser Name and Version in 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, laravel 9, laravel 10 and laravel 11 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to use Carbon in Laravel Blade or Controller File?
- Laravel Money/Currency Format Example
- How to Convert Number to Words in Laravel?
- How to Store Array in Database Laravel?
- Laravel 9 Livewire CRUD using Jetstream & Tailwind CSS
- Laravel Search Case Insensitive Query Example
- Laravel Convert PDF to Image Example
- Laravel Include Blade File with Data Example
- Laravel Livewire Delete Confirmation Example
- Laravel Carbon addSeconds() | Laravel Carbon Add Seconds Example
- Laravel Validation Allow Only Numbers Example
- How to Get Query Log in Laravel Eloquent?