Views and Blade Templates

Views are stored in the resources/views/ directory and have the .blade.php extension.

Creating a simple view

File: resources/views/home.blade.php

<html>
<head>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>

Registering the view in a route

Route::view('/home','home')

Passing data to the view

return view('home', ['name' => $name]);

In the Blade file:

<div>{{$name}}</div>

Blade Directives

Directive Description
{{ $variable }} Outputs a variable
@if( condition ) Conditional statement
@foreach($items as $item)
<div>{{$item->id}}</div>
@endforeach
Loop through items
@include('view') Includes another view
@extends('layout') Uses a main layout file
@section('content') Defines a content section
@isset('content') Checks if a variable is set
@empty('content') Checks if a variable is empty

Example Layout layouts/app.blade.php

<html>
<head>
</head>
<body>
<h1>This is the same on every page that extends the layout</h1>
@section('content')
@show
</body>
</html>

View using the layout home.blade.php

@extends('layout')
@section('content')
This is different on every page
@endsection