The most reputable Laravel package provided for detecting user agent is Agent developed by Jens Segers. All the information you need to know about user are accessible by using Agent (jenssegers/agent) package. Agent utilizes the Mobile Detect PHP Class under the hood. Agent makes Laravel equipped to detect the user environment, i.e:
1- Detecting user platform and its version: Including Windows, Ubuntu, OS X, AndroidOS, etc.
2- Detecting user device and device name: Including mobile, tablet, desktop and it will get its name (iPhone, Nexus, AsusTablet, …).
3- Detecting user browser and its version: Including Firefox, Chrome, Safari, IE, etc.
4- And also detecting user language.
How to implement:
First of all do this:
composer require jenssegers/agent
Then find the config/app.php and then find providers key as below:
Then add this line as the Agent service provider to the array of providers:
Then in config/app.php find the aliases key as below:
add an alias for Agent to the array of aliases like:
'Agent' => Jenssegers\Agent\Facades\Agent::class,
How to use:
First, put this line at the top of the controller:
Then start by creating an Agent instance (or use the Agent Facade).
$agent = new Agent();
// Ubuntu, Windows, OS X, ...$agent->browser();
// Chrome, IE, Safari, Firefox, ...$browser = $agent->browser();
$version = $agent->version($browser);$platform = $agent->platform();
$version = $agent->version($platform);
// ['nl-nl', 'nl', 'en-us', 'en']$agent->device();
// iPhone, Nexus, AsusTablet, ...$agent->isDesktop();
// robot name
More examples and original methods can be find here:
Blade view file usage:
If you pass the $agent to a specific view blade file you can do this:
Show content for mobile ...
Global Blade view file usage:
Moreover you can share
$agent and make it available inside all of your view blade files in this way:
Make AgentServiceProvider.php in app/providers/ directory by :
php artisan make:provider AgentServiceProvider
Then register this provider (the below line) in config/app.php file in the array of providers as described before:
Then fill the AgentServiceProvider.php with:
class AgentServiceProvider extends ServiceProvider
public function boot()
$agent = new Agent();
public function register()
Then inside the view blade file use this:
Show content for mobile ...@endif
Some other useful packages:
Thank you for reading! If you enjoyed this article:
Clap it ! Share it! Follow Me in Medium!
Panjeh — Medium
Read writing from Panjeh on Medium. Physicists PhD, Self-taught web developer #Learning, #laravelphp #Vuejs. Every day…
Also I’d like to hear your opinion on this article. If you have any doubt, question or suggestion please leave a comment below.
Have a very wonderful day!