PHPackages                             atldays/laravel-agent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. atldays/laravel-agent

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

atldays/laravel-agent
=====================

Laravel package for parsing user-agent strings into typed browser, operating system, device, and bot objects.

v2.0.0(1mo ago)0351MITPHPPHP ^8.2CI passing

Since Apr 17Pushed 1mo agoCompare

[ Source](https://github.com/atldays/laravel-agent)[ Packagist](https://packagist.org/packages/atldays/laravel-agent)[ Docs](https://github.com/atldays/laravel-agent)[ RSS](/packages/atldays-laravel-agent/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (2)Dependencies (10)Versions (4)Used By (1)

Laravel Agent
=============

[](#laravel-agent)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5fc933a4be45e40a9be846d19356a26f6d64f73790c2df99b76b9f8b4a28e360/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61746c646179732f6c61726176656c2d6167656e742e7376673f6c6f676f3d7061636b6167697374267374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/atldays/laravel-agent)[![Total Downloads](https://camo.githubusercontent.com/db66b5bfa0209dced848aaab6146c368afb39fb4420b480d70ec2c6aabd2af5f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f61746c646179732f6c61726176656c2d6167656e742e7376673f7374796c653d666f722d7468652d626164676526636f6c6f723d626c7565)](https://packagist.org/packages/atldays/laravel-agent/stats)[![CI](https://camo.githubusercontent.com/e1f766022dd1bfe8d42a78130fd307706a1f36e60999948a95f4f0462e9a33e0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f61746c646179732f6c61726176656c2d6167656e742f63692e796d6c3f7374796c653d666f722d7468652d6261646765266c6162656c3d4349)](https://github.com/atldays/laravel-agent/actions/workflows/ci.yml)[![License: MIT](https://camo.githubusercontent.com/7a1226d14a365d288bfe51ece915ee0c7e754a16faa51ff06436504de29b33b4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e7376673f7374796c653d666f722d7468652d6261646765)](LICENSE.md)

`atldays/laravel-agent` is a Laravel package for parsing user-agent strings into clean, typed objects.

It gives you a comfortable Laravel-first API for working with:

- browsers
- operating systems
- devices
- bots
- request user-agent strings

Under the hood, the package uses [`matomo/device-detector`](https://github.com/matomo-org/device-detector), while exposing a clean developer experience through Laravel service container bindings, a request macro, facades, model helpers, and typed DTOs powered by `spatie/laravel-data`.

Features
--------

[](#features)

- Laravel auto-discovery support
- `request()->agent()` request macro
- `Agent` facade for the current request agent
- `AgentManager` facade for on-demand detection
- dependency injection support via `AgentContract`
- container binding for `AgentContract`
- typed DTO objects for browser, OS, device, bot, and producer data
- Eloquent cast for persisted user-agent strings
- model trait for convenient access to parsed agents
- bot-blocking middleware alias
- test coverage for real user-agent fixtures and framework integration

Requirements
------------

[](#requirements)

- PHP `^8.2`
- Laravel `^11.0|^12.0|^13.0`

Installation
------------

[](#installation)

```
composer require atldays/laravel-agent
```

The package supports Laravel package auto-discovery, so no manual provider registration is required.

Quick Start
-----------

[](#quick-start)

### Request macro

[](#request-macro)

```
use Illuminate\Http\Request;

Route::get('/', function (Request $request) {
    $agent = $request->agent();

    return [
        'user_agent' => $agent->userAgent(),
        'hash' => $agent->hash(),
        'browser' => $agent->browser()?->name(),
        'os' => $agent->os()?->name(),
        'device' => $agent->device()?->device(),
        'is_bot' => $agent->isBot(),
    ];
});
```

### Facades

[](#facades)

```
use Atldays\Agent\Facades\Agent;
use Atldays\Agent\Facades\AgentManager;

$currentAgent = AgentManager::request();
$customAgent = AgentManager::detect($userAgent);

$browser = Agent::browser();
$os = Agent::os();
$device = Agent::device();
$bot = Agent::bot();
```

### Dependency injection

[](#dependency-injection)

```
use Atldays\Agent\Contracts\AgentContract;

class SomeAction
{
    public function __invoke(AgentContract $agent): array
    {
        return [
            'browser' => $agent->browser()?->name(),
            'device' => $agent->device()?->device(),
            'is_bot' => $agent->isBot(),
        ];
    }
}
```

Core API
--------

[](#core-api)

The main parsed object is `Atldays\Agent\Contracts\AgentContract`.

Available methods:

- `userAgent(): string`
- `hash(): string`
- `browser(): ?BrowserContract`
- `os(): ?OsContract`
- `device(): ?DeviceContract`
- `bot(): ?BotContract`
- `isBot(): bool`
- `toArray(): array`

Example:

```
$agent = request()->agent();

if ($agent->isBot()) {
    // ...
}

$browser = $agent->browser();
$os = $agent->os();
$device = $agent->device();
```

DTO Objects
-----------

[](#dto-objects)

The package returns typed DTO objects instead of raw arrays.

### Browser DTO

[](#browser-dto)

Returned by:

- `$agent->browser()`

Class:

- `Atldays\Agent\Data\Browser`

Available data methods:

- `name(): string`
- `shortName(): ?string`
- `version(): ?string`
- `family(): ?string`
- `engine(): ?string`
- `engineVersion(): ?string`

Available helper methods:

- `isChrome(): bool`
- `isEdge(): bool`
- `isFirefox(): bool`
- `isOpera(): bool`
- `isSafari(): bool`

Example:

```
$browser = request()->agent()->browser();

if ($browser?->isEdge()) {
    // ...
}
```

### OS DTO

[](#os-dto)

Returned by:

- `$agent->os()`

Class:

- `Atldays\Agent\Data\Os`

Available data methods:

- `name(): string`
- `shortName(): ?string`
- `version(): ?string`
- `family(): ?string`
- `platform(): ?string`

Available helper methods:

- `isApple(): bool`
- `isAndroid(): bool`
- `isIos(): bool`
- `isLinux(): bool`
- `isMacOs(): bool`
- `isWindows(): bool`

Example:

```
$os = request()->agent()->os();

if ($os?->isLinux()) {
    // ...
}
```

### Device DTO

[](#device-dto)

Returned by:

- `$agent->device()`

Class:

- `Atldays\Agent\Data\Device`

Available data methods:

- `type(): DeviceType`
- `device(): string`
- `brand(): ?string`
- `model(): ?string`

Available helper methods:

- `isDesktop(): bool`
- `isMobile(): bool`
- `isTablet(): bool`
- `isPhone(): bool`
- `isApple(): bool`
- `isIphone(): bool`

Example:

```
$device = request()->agent()->device();

if ($device?->isTablet()) {
    // ...
}
```

### Bot DTO

[](#bot-dto)

Returned by:

- `$agent->bot()`

Class:

- `Atldays\Agent\Data\Bot`

Available data methods:

- `name(): string`
- `category(): ?string`
- `url(): ?\Atldays\Url\Contracts\Url`
- `producer(): ?ProducerContract`

### Producer DTO

[](#producer-dto)

Returned by:

- `$agent->bot()?->producer()`

Class:

- `Atldays\Agent\Data\Producer`

Available data methods:

- `name(): ?string`
- `url(): ?\Atldays\Url\Contracts\Url`

Manual Detection
----------------

[](#manual-detection)

If you already have a user-agent string, you can parse it directly:

```
use Atldays\Agent\Facades\AgentManager;

$agent = AgentManager::detect($userAgent);

$browserName = $agent->browser()?->name();
$isAndroid = $agent->os()?->isAndroid();
$isMobile = $agent->device()?->isMobile();
```

You can also resolve the manager directly:

```
use Atldays\Agent\AgentManager;

$manager = app(AgentManager::class);

$agent = $manager->detect($userAgent);
```

Eloquent Integration
--------------------

[](#eloquent-integration)

### Agent cast

[](#agent-cast)

Use `AgentCast` when your model stores a raw `user_agent` string but you want to work with an `AgentContract`.

```
use Atldays\Agent\Casts\AgentCast;
use Illuminate\Database\Eloquent\Model;

class Visit extends Model
{
    protected function casts(): array
    {
        return [
            'user_agent' => AgentCast::class,
        ];
    }
}
```

Now the attribute will resolve to an `AgentContract`:

```
$visit->user_agent->browser()?->name();
$visit->user_agent->device()?->isMobile();
```

### `HasAgent` trait

[](#hasagent-trait)

Use the trait when you want a dedicated `agent()` helper on your model.

```
use Atldays\Agent\Concerns\HasAgent;
use Illuminate\Database\Eloquent\Model;

class Visit extends Model
{
    use HasAgent;
}
```

Example:

```
$visit->agent()->browser()?->name();
$visit->agent()->os()?->isMacOs();
$visit->agent()->device()?->isPhone();
```

By default the trait reads from the `user_agent` column. You can override that:

```
protected function getUserAgentColumn(): string
{
    return 'agent_string';
}
```

Middleware
----------

[](#middleware)

The package registers the `agent.block-bots` middleware alias.

### Block all bots

[](#block-all-bots)

```
Route::middleware('agent.block-bots')->group(function () {
    // ...
});
```

### Allow selected bots

[](#allow-selected-bots)

```
Route::middleware('agent.block-bots:Googlebot,Bingbot')->group(function () {
    // ...
});
```

If a request is detected as a bot and is not allow-listed, the middleware throws a `403 Forbidden` response.

Testing
-------

[](#testing)

Run the test suite:

```
composer test
```

Run formatting:

```
composer format
composer format:test
```

Run both:

```
composer check
```

Why Laravel Agent
-----------------

[](#why-laravel-agent)

Most user-agent packages stop at “parse a string into an array”.

`laravel-agent` is built to feel native inside a Laravel application:

- typed objects instead of loose arrays
- request-first API for day-to-day usage
- model integration for persisted user agents
- framework-friendly container bindings and facade support
- focused helper methods on DTOs for browser, OS, and device checks

License
-------

[](#license)

The MIT License (MIT). Please see [LICENSE.md](LICENSE.md) for more information.

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance90

Actively maintained with recent releases

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

2

Last Release

50d ago

Major Versions

v1.0.0 → v2.0.02026-04-20

### Community

Maintainers

![](https://www.gravatar.com/avatar/d3d0138ffb1733c52bcd1602597088817ed576f8f9489a3b58095cf5c84264ca?d=identicon)[atldays](/maintainers/atldays)

---

Top Contributors

[![atldays](https://avatars.githubusercontent.com/u/130153594?v=4)](https://github.com/atldays "atldays (16 commits)")

---

Tags

botbot-detectionbrowserdevicedevice-detectionlaravelosuser-agentuser-agent-parserbrowserlaraveluser agentbotdeviceoperating systemdevice detectoratldays

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/atldays-laravel-agent/health.svg)

```
[![Health](https://phpackages.com/badges/atldays-laravel-agent/health.svg)](https://phpackages.com/packages/atldays-laravel-agent)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3325.1M337](/packages/psalm-plugin-laravel)[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k51.0M7.4k](/packages/larastan-larastan)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

45344.0k1](/packages/pressbooks-pressbooks)[laravel/ai

The official AI SDK for Laravel.

9782.1M153](/packages/laravel-ai)[simplestats-io/laravel-client

Analytics for Laravel. Track visitors, registrations, and payments. Discover which channels actually drive revenue, not just traffic. Server-side, GDPR compliant, ad-blocker proof.

5019.3k](/packages/simplestats-io-laravel-client)[api-platform/laravel

API Platform support for Laravel

59156.3k10](/packages/api-platform-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
