Laravel detect device mobile or desktop example; In this tutorial we are going to share how to dedect device is Mobile or Laptop or Desktop in Laravel. For detecting mobile, desktop, laptop phone etc is required sometime in laravel, So we are use jenssegers/agent for easily detecting any device.
You need to just follow bellow tutorial to detect mobile or desktop access device.
How to Detect Device in Laravel
Follow the following steps to check is user which device to opening the app e.g. mobile or laptop or desktop:
- Step 1: Install Laravel App
- Step 2: Connect Database To App
- Step 3: Install & Configure jenssegers Package
- Step 4: Add Routes For Detect Device
- Step 5: Create Controller
- Step 6: Use in Blade View
Install Laravel App
On the console, screen type the following command and hit enter to generate a new laravel project. You can ignore this step if the app is already installed:
composer create-project --prefer-dist laravel/laravel laravel-detect-device
Go into the app:
cd laravel-detect-device
Connect Database to App
After successfully install laravel new application, Go to project root directory and open .env file. Then set up database credential in .env file as follow:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=root
DB_PASSWORD=
Install & Configure jenssegers Package
Next, you need to install jessenger/ajent package by composer using the the following command:
composer require jenssegers/agent
Now we need to set provider and alias. Navigate to config/app.php and add Jenssegers classes to providers and aliases like this:
config/app.php
'providers' => [
....
Jenssegers\Agent\AgentServiceProvider::class,
]
'aliases' => [
....
'Agent' => Jenssegers\Agent\Facades\Agent::class,
]
Create Routes for Detect Debice
Now create routes to detect devices. Open routes/web.php and create routes like these:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FrontController;
Route::get('detect-device', [FrontController::class, 'detuctDebice'])->name('detect-device');
Create Controller
Open terminal, type command, then execute the command that generates a new controller
php artisan make:controller FrontController
Open and add code in app/http/controllers/FrontController.php file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Jenssegers\Agent\Agent;
class FrontController extends Controller
{
public function detuctDebice()
{
$agent = new Agent;
$mobileResult = $agent->isMobile();
if ($mobileResult) {
$result = 'Yes, This is Mobile.';
}
$desktopResult= $agent->isDesktop();
if ($desktopResult) {
$result = 'Yes, This is Desktop.';
}
$tabletResult= $agent->isTablet();
if ($tabletResult) {
$result = 'Yes, This is Desktop.';
}
$tabletResult= $agent->isPhone();
if ($tabletResult) {
$result = 'Yes, This is Phone.';
}
dd($result);
}
}
Use in Blade File
You can use the device detector package easily in laravel blade file just like below.
@if((new \Jenssegers\Agent\Agent())->isDesktop())
{{-- your code --}}
@endif
@if((new \Jenssegers\Agent\Agent())->isMobile())
{{-- your code --}}
@endif
I hope you enjoy the laravel detect device mobile, laptop or desktop etc steps…