Skip to content  


Get started with PHP and Laravel faster than ever using Laravel Herd.  


Join us in Dallas, TX! Tickets are now available for Laracon US.  


Let's go to India! Tickets are now available for Laracon IN.  


Let's go to Europe! Tickets are now available for Laracon EU.  


Let's go down under! Tickets are now available for Laracon AU.  


Servers with PHP 8.3 are now available for provisioning via Laravel Forge.  


Deploy Laravel with the infinite scale of serverless using Laravel Vapor.  


How's your health? Check your application's vital signs using Laravel Pulse.  


Incoming transmission received. Laravel Reverb is now available!  


Take your administration backend to another dimension with Laravel Nova.  


Laravel is hiring! Help us build the future of Laravel.  







Preface



Introduction

Quickstart

Release Notes

Upgrade Guide

Contribution Guide



Getting Started



Installation

Configuration

Homestead

Request Lifecycle

Routing

Requests & Input

Views & Responses

Controllers

Errors & Logging



Learning More



Authentication

Billing

Cache

Core Extension

Events

Facades

Forms & HTML

Helpers

IoC Container

Localization

Mail

Package Development

Pagination

Queues

Security

Session

SSH

Templates

Unit Testing

Validation



Database



Basic Usage

Query Builder

Eloquent ORM

Schema Builder

Migrations & Seeding

Redis



Artisan CLI



Overview

Development



 

Laravel Forge: create and manage PHP 8 servers. Deploy your Laravel applications in seconds. Sign up now!.  

Laravel Vapor: experience extreme scale on a dedicated serverless platform for Laravel. Sign up now!.  

Laravel Nova: The next generation of Nova is now available.  

Laravel Pulse: How's your health? Check your application's vital signs using Laravel Pulse.  

Laravel Reverb: You can easily build dynamic, real-time Laravel applications using WebSockets. Laravel Reverb is now available!  










Preface



Introduction

Quickstart

Release Notes

Upgrade Guide

Contribution Guide



Getting Started



Installation

Configuration

Homestead

Request Lifecycle

Routing

Requests & Input

Views & Responses

Controllers

Errors & Logging



Learning More



Authentication

Billing

Cache

Core Extension

Events

Facades

Forms & HTML

Helpers

IoC Container

Localization

Mail

Package Development

Pagination

Queues

Security

Session

SSH

Templates

Unit Testing

Validation



Database



Basic Usage

Query Builder

Eloquent ORM

Schema Builder

Migrations & Seeding

Redis



Artisan CLI



Overview

Development



 





 












exclamation  


WARNING You're browsing the documentation for an old version of Laravel.  Consider upgrading your project to Laravel 11.x.  




Templates



Controller Layouts

Blade Templating

Other Blade Control Structures

Extending Blade


Controller Layouts


One method of using templates in Laravel is via controller layouts. By specifying the layout property on the controller, the view specified will be created for you and will be the assumed response that should be returned from actions.

Defining A Layout On A Controller

class UserController extends BaseController {
 
/**
* The layout that should be used for responses.
*/
protected $layout = 'layouts.master';
 
/**
* Show the user profile.
*/
public function showProfile()
{
$this->layout->content = View::make('user.profile');
}
 
}

Blade Templating


Blade is a simple, yet powerful templating engine provided with Laravel. Unlike controller layouts, Blade is driven by template inheritance and sections. All Blade templates should use the .blade.php extension.

Defining A Blade Layout

<!-- Stored in app/views/layouts/master.blade.php -->
 
<html>
<body>
@section('sidebar')
This is the master sidebar.
@show
 
<div class="container">
@yield('content')
</div>
</body>
</html>

Using A Blade Layout

@extends('layouts.master')
 
@section('sidebar')
@@parent
 
<p>This is appended to the master sidebar.</p>
@stop
 
@section('content')
<p>This is my body content.</p>
@stop

Note that views which extend a Blade layout simply override sections from the layout. Content of the layout can be included in a child view using the @parent directive in a section, allowing you to append to the contents of a layout section such as a sidebar or footer.

Sometimes, such as when you are not sure if a section has been defined, you may wish to pass a default value to the @yield directive. You may pass the default value as the second argument:
@yield('section', 'Default Content')

Other Blade Control Structures

Echoing Data

Hello, {{{ $name }}}.
 
The current UNIX timestamp is {{{ time() }}}.

Echoing Data After Checking For Existence


Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. Basically, you want to do this:
{{{ isset($name) ? $name : 'Default' }}}

However, instead of writing a ternary statement, Blade allows you to use the following convenient short-cut:
{{{ $name or 'Default' }}}

Displaying Raw Text With Curly Braces


If you need to display a string that is wrapped in curly braces, you may escape the Blade behavior by prefixing your text with an @ symbol:
@{{ This will not be processed by Blade }}

Of course, all user supplied data should be escaped or purified. To escape the output, you may use the triple curly brace syntax:
Hello, {{{ $name }}}.

If you don't want the data to be escaped, you may use double curly-braces:
Hello, {{ $name }}.



lightbulb
 

Be very careful when echoing content that is supplied by users of your application. Always use the triple curly brace syntax to escape any HTML entities in the content.

If Statements

@if (count($records) === 1)
I have one record!
@elseif (count($records) > 1)
I have multiple records!
@else
I don't have any records!
@endif
 
@unless (Auth::check())
You are not signed in.
@endunless

Loops

@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
@endfor
 
@foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
 
@forelse($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse
 
@while (true)
<p>I'm looping forever.</p>
@endwhile

Including Sub-Views

@include('view.name')

You may also pass an array of data to the included view:
@include('view.name', array('some'=>'data'))

Overwriting Sections


To overwrite a section entirely, you may use the overwrite statement:
@extends('list.item.container')
 
@section('list.item.content')
<p>This is an item of type {{ $item->type }}</p>
@overwrite

Displaying Language Lines

@lang('language.line')
 
@choice('language.line', 1)

Comments

{{-- This comment will not be in the rendered HTML --}}

Extending Blade


Blade even allows you to define your own custom control structures. When a Blade file is compiled, each custom extension is called with the view contents, allowing you to do anything from simple str_replace manipulations to more complex regular expressions.

The Blade compiler comes with the helper methods createMatcher and createPlainMatcher, which generate the expression you need to build your own custom directives.

The createPlainMatcher method is used for directives with no arguments like @endif and @stop, while createMatcher is used for directives with arguments.

The following example creates a @datetime($var) directive which simply calls ->format()on$var:
Blade::extend(function($view, $compiler)
{
$pattern = $compiler->createMatcher('datetime');
 
return preg_replace($pattern, '$1<?php echo $2->format(\'m/d/Y H:i\'); ?>', $view);
});
 



Laravel


Laravel is a web application framework with expressive, elegant syntax. We believe development must  be an enjoyable and creative experience to be truly fulfilling. Laravel attempts to take the pain  out of development by easing common tasks used in most web projects.





YouTube


Highlights  


Release Notes  

Getting Started  

Routing  

Blade Templates  

Authentication  

Authorization  

Artisan Console  

Database  

Eloquent ORM  

Testing  




Resources  


Laravel Bootcamp  

Laracasts  

Laravel News  

Laracon  

Laracon AU  

Laracon EU  

Laracon India  

Larabelles  

Careers  

Jobs  

Forums  

Trademark  




Partners  


Vehikl  

WebReinvent  

Tighten  

Bacancy  

64 Robots  

Active Logic  

Black Airplane  

Byte 5  

Curotec  

Cyber-Duck  

DevSquad  

Jump24  

Kirschbaum  




Ecosystem  


Breeze  

Cashier  

Dusk  

Echo  

Envoyer  

Forge  

Herd  

Horizon  

Inertia  

Jetstream  

Livewire  

Nova  

Octane  

Pennant  

Pint  

Prompts  

Pulse  

Reverb  

Sail  

Sanctum  

Scout  

Socialite  

Spark  

Telescope  

Vapor  






Laravel is a Trademark of Laravel Holdings Inc.
 Copyright © 2011-2024 Laravel Holdings Inc.  

Code highlighting provided by Torchlight