Laravel detect mobile/tablet and load correct views

For you Laravel 5 users in the future who are looking for a way to detect devices in the view; another option is to create a ServiceProvier - and then use View::share() - which will then make the device-detecting $agent available inside all of your views.

Install Agent

composer require jenssegers/agent

Create the service provider

php artisan make:provider AgentServiceProvider

In config/app.php

App\Providers\AgentServiceProvider::class,

in app/providers/AgentServiceProvider.php

<?php

namespace App\Providers;

use View;
use Jenssegers\Agent\Agent;
use Illuminate\Support\ServiceProvider;

class AgentServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $agent = new Agent();

        View::share('agent', $agent);
    }

    public function register()
    {
        //
    }
}

Then inside your views

@if ($agent->isMobile())

    Show mobile stuff...

@endif

Refer: https://stackoverflow.com/questions/23779088/laravel-detect-mobile-tablet-and-load-correct-views