How to Get Browser Name and Version in Laravel 9?

Hello dev, Today we are going to learn on How to Get Browser Name and Version in Laravel 9? This tutorial will cover on how you can get the browser’s name & versions of visitor’s in laravel application.

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 a do, let’s see one by one example:

Install a 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

Examples on How to Get Browser Name and Version in Laravel 9:

Also Read: Laravel 9 Custom Login and Registration Example

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

Also Read: How to Integrating AdminLTE 3 in Laravel 9?

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

Also Read: Laravel 9 return json From Controller Example

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.

Conclusion:

Today, We had learn How to Get Browsers Name and Version in Laravel 9?. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: Laravel 9 Get Current Route Name Example

1 Comment

Leave a Reply