Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Laravel detect mobile/tablet and load correct views

Check user is from mobile and desktop without a package in Laravel

Do you want to detect and check if the user is from mobile and desktop without a package in Laravel?

Check user is from mobile and desktop without a package in Laravel

Detect Devices is Mobile or Desktop in Laravel

This step-by-step tutorial helps you to detect the user agent type and also check which device is used by a user with the help of the PHP function in the Laravel application.

Check user is from mobile and desktop without a package in Laravel

Sometimes we need to manage data according to multiple devices, like if we have an e-commerce application, and we want to show different image sliders on the desktop and different image sliders on mobile devices, there are two ways to achieve your goal, one way is by using the jenssegers/agent Laravel package, and another way is without using the package.

In this article, we use the second option, without using the package for Laravel to get device info.

Route :

<?php

use Illuminate\Support\Facades\Route;

Route::view('/device-type','device');

Blade File :

@php
function isMobileDevice(){
    if(!empty($_SERVER['HTTP_USER_AGENT'])){
       $user_ag = $_SERVER['HTTP_USER_AGENT'];
       if(preg_match('/(Mobile|Android|Tablet|GoBrowser|[0-9]x[0-9]*|uZardWeb\/|Mini|Doris\/|Skyfire\/|iPhone|Fennec\/|Maemo|Iris\/|CLDC\-|Mobi\/)/uis',$user_ag)){
          return true;
       };
    };
    return false;
}
@endphp

<!-- Use the function -->

@if(isMobileDevice())
    <h2>Mobile Users </h2>
@else 
    <h2> Desktop Users </h2>
@endif

I hope that this article helped you learn How to detect mobile/tablet and load correct views in Laravel, with the help of the PHP (HTTP_USER_AGENT) function example.

You can also add the above code in your helper and then call it anywhere you need to check, the response is coming from which device. You may also want to check out our guide on how to Store date, month, and year with different dropdowns in a single column in Laravel.