Skip to main content

Documentation Index

Fetch the complete documentation index at: https://kawax.biz/llms.txt

Use this file to discover all available pages before exploring further.

Introduction

Returning an entire HTML document as a string from a route or controller is not practical. Views let you keep all your HTML in separate files. Views separate your controller and application logic from your presentation logic. Laravel stores views in the resources/views directory. By convention, Laravel views are written using the Blade templating language.
<!-- resources/views/greeting.blade.php -->
<html>
    <body>
        <h1>Hello, {{ $name }}</h1>
    </body>
</html>
This view is stored at resources/views/greeting.blade.php and you can return it using the global view helper.
Route::get('/', function () {
    return view('greeting', ['name' => 'James']);
});

Creating views

To create a view, place a file with a .blade.php extension in the resources/views directory, or use the Artisan command:
php artisan make:view greeting
The .blade.php extension tells the framework that the file contains a Blade template.

Returning views

Once you have created a view, you can return it from a route or controller using the global view helper:
Route::get('/', function () {
    return view('greeting', ['name' => 'James']);
});
You can also use the View facade:
use Illuminate\Support\Facades\View;

return View::make('greeting', ['name' => 'James']);
The first argument to view corresponds to the name of the view file in the resources/views directory. The second argument is an array of data to make available to the view.

Nested view directories

Views can be nested inside subdirectories of resources/views. Use dot notation to reference nested views. For example, a view stored at resources/views/admin/profile.blade.php can be returned like this:
return view('admin.profile', $data);
Do not include a . character in your view directory names.

Returning the first existing view

Use the first method on the View facade to return the first view that exists from an array:
use Illuminate\Support\Facades\View;

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

Checking if a view exists

Use the exists method on the View facade to check whether a view exists:
use Illuminate\Support\Facades\View;

if (View::exists('admin.profile')) {
    // ...
}

Passing data to views

As shown in the examples above, you pass an array of data to a view to make it available inside:
return view('greetings', ['name' => 'Victoria']);
The data must be a key/value array. Inside the view, you can access each value using its corresponding key. You can also add individual pieces of data using the with method. It returns the view instance, so you can chain calls:
return view('greeting')
    ->with('name', 'Victoria')
    ->with('occupation', 'Engineer');

Sharing data with all views

To share data with every view in your application, use the share method on the View facade. Call it in the boot method of a service provider:
<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        View::share('appName', 'MyApp');
    }
}

Optimizing views

By default, Blade templates are compiled on demand. When a request renders a view, Laravel checks whether a compiled version exists and whether the template has been modified. You can pre-compile all views using the view:cache Artisan command — this is recommended as part of your deployment process:
php artisan view:cache
To clear the view cache:
php artisan view:clear

Next steps

Blade templates

Learn how to use Blade syntax to build dynamic views.
Last modified on March 29, 2026