Laravel Views – Blade Templates, Data Sharing & Optimization

aravel Laravel Views & Blade Templates – Beginner’s Guide

Introduction

In Laravel, returning HTML directly from routes or controllers is not practical. Instead, Laravel provides views to keep HTML code separate from application logic.

  • Views live inside the resources/views directory.
  • By default, Laravel views use the Blade templating engine (.blade.php extension).
  • Blade allows writing plain HTML mixed with dynamic data.

Example:

resources/views/greeting.blade.php

<html>
  <body>
    <h1>Hello, {{ $name }}</h1>
  </body>
</html>

routes/web.php

Route::get('/', function () {
    return view('greeting', ['name' => 'James']);
});

Here, view('greeting', [...]) loads the file resources/views/greeting.blade.php and passes data to it.


Writing Views in React / Vue

Laravel is not limited to Blade.
You can also write your frontend in React or Vue using Inertia.js.

  • Inertia ties your Laravel backend to a modern JS frontend.
  • Starter kits (laravel/breeze or jetstream) make setup easy.

So you can choose:

  • Blade templates → PHP-based views.
  • React/Vue with Inertia → Full SPA-like frontend.

Creating and Rendering Views

Creating a View

You can create views manually or via Artisan:

php artisan make:view greeting

This creates resources/views/greeting.blade.php.

Rendering Views

Return views from routes or controllers:

return view('greeting', ['name' => 'James']);

Or using the View facade:

use Illuminate\Support\Facades\View;

return View::make('greeting', ['name' => 'James']);

Nested View Directories

You can organize views inside folders.

resources/views/admin/profile.blade.php

<h1>Admin Profile</h1>

Return it like this:

return view('admin.profile', $data);

Use dot notation (admin.profile) instead of slashes.


Creating the First Available View

If you want to load the first existing view from a list:

use Illuminate\Support\Facades\View;

return View::first(['custom.admin', 'admin'], $data);

This checks for resources/views/custom/admin.blade.php first,
if missing → loads resources/views/admin.blade.php.


Checking If a View Exists

Before rendering:

use Illuminate\Support\Facades\View;

if (View::exists('admin.profile')) {
    return view('admin.profile');
}

Passing Data to Views

Method 1: Array
return view('greeting', ['name' => 'Victoria']);

Inside Blade:

Hello, {{ $name }}
Method 2: with()
return view('greeting')
    ->with('name', 'Victoria')
    ->with('occupation', 'Astronaut');

Sharing Data With All Views

If you want data to be available in every view, use View::share().

App\Providers\AppServiceProvider.php

use Illuminate\Support\Facades\View;

public function boot(): void
{
    View::share('appName', 'My Laravel App');
}

Now {{ $appName }} is available in all views.


View Composers

Sometimes you want to bind dynamic data to specific views whenever they are rendered.

  • View composers let you attach logic to views.
  • They are defined in Service Providers.

App\Providers\AppServiceProvider.php

use App\View\Composers\ProfileComposer;
use Illuminate\Support\Facades\View;

public function boot(): void
{
    // Class-based composer
    View::composer('profile', ProfileComposer::class);

    // Closure-based composer
    View::composer('dashboard', function ($view) {
        $view->with('count', 5);
    });
}

App\View\Composers\ProfileComposer.php

namespace App\View\Composers;

use App\Repositories\UserRepository;
use Illuminate\View\View;

class ProfileComposer
{
    public function __construct(protected UserRepository $users) {}

    public function compose(View $view): void
    {
        $view->with('count', $this->users->count());
    }
}

This way, count will always be available in the profile view.

Multiple Views
View::composer(['profile', 'dashboard'], ProfileComposer::class);
All Views
View::composer('*', function ($view) {
    $view->with('globalVar', 'Available Everywhere');
});

View Creators

  • Similar to composers.
  • But they run immediately after a view is created, not just before rendering.
use App\View\Creators\ProfileCreator;
use Illuminate\Support\Facades\View;

View::creator('profile', ProfileCreator::class);

Optimizing Views

Blade templates are compiled into cached PHP files.

  • On first load → Laravel compiles the .blade.php file.
  • Next requests → Laravel uses the cached version.
Precompile Views (Deployment Step)
php artisan view:cache
Clear Cached Views
php artisan view:clear

Important

  • Views separate HTML from PHP logic.
  • Stored in resources/views (Blade by default).
  • Support React/Vue with Inertia.
  • Use view() helper or View::make() to render.
  • Organize with nested directories.
  • Pass data via arrays, with(), or View::share().
  • Use View Composers & Creators for dynamic/global data.
  • Optimize performance with view:cache.