Agents Detector

Agents Detector

Laravel Agents Detector (Devices, Operation Systems, Browsers, Bots).

Moamen Eltouny (Raggi)
PHP Version : >= 7.2 Laravel Version : >= 6.0 License Support
Source Packagist Version Packagist Downloads

Install

Install the latest version using Composer

composer require pharaonic/laravel-agents-detector
php artisan migrate

OptionalPublish the config file.

php artisan vendor:publish --tag=laravel-agents
Usage

An example of extracting the agent's OS name using agent helper.

echo agent()->os->name;    // Windows

Dump agent helper

Pharaonic\Laravel\Agents\Classes\AgentDetector { ▼
    +agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"
    +ip: "127.0.0.1"
    +languages: array:3 [▼
        0 => array:2 [▼
            0 => "en"
            1 => "US"
        ]
        1 => "en"
        2 => "ar"
    ]
    +language: "en"
    +variant: "US"
    +"id": 1
    +"isBot": false
    +"bot": null
    +"type": "browser"
    +"device": { ▼
        +"type": "desktop"
        +"brand": null
        +"model": null
    }
    +"os": { ▼
        +"family": "Windows"
        +"name": "Windows"
        +"platform": "x64"
    }
    +"browser": { ▼
        +"family": "Chrome"
        +"name": "Chrome"
        +"engine": "Blink"
    }
}

Also you can use agent middleware to auto detect the agents and store it in Database without displaying it immediately.

Route::middleware('agent')->get('/', function () {
    // ...
    return 'Agent has been detected!';
});
Models

Simple and Traditional ways to manipulate with Models.

use Pharaonic\Laravel\Agents\Models\Agent;
use Pharaonic\Laravel\Agents\Models\Bot;
use Pharaonic\Laravel\Agents\Models\Browser;
use Pharaonic\Laravel\Agents\Models\Device;
use Pharaonic\Laravel\Agents\Models\OperationSystem;


$agent = Agent::with('bot', 'browser', 'device', 'operationSystem')->first();

if (!$agent->is_bot) {
    dd(
        $agent->device,
        $agent->operationSystem,
        $agent->browser
    );
}


$bots = Bot::all();

$browsers = Browser::all();
$devices = Device::all();
$operationSystems = OperationSystem::all();