In Laravel, views are used to show content to users.
Views help us display HTML pages like home pages, about pages, and forms.
Laravel uses a powerful template system called Blade to make views easy and clean.
What Are Views?
A view is a file that contains HTML code.
Views are stored inside this folder:
resources/views
Example of a simple view file:
home.blade.php
Inside the file:
<h1>Welcome to Laravel</h1> <p>This is my first view</p>
Views are used to:
- Show pages to users
- Display data
- Keep design separate from logic
What is Blade Template?
Blade is Laravel’s template engine.
Blade makes HTML:
- Cleaner
- More powerful
- Easy to read
Blade files use this extension:
.blade.php
Blade allows us to:
- Display variables
- Use conditions
- Loop data easily
Blade Syntax: Displaying Data ({{ }})
To show data in a Blade view, we use:
{{ }}
Example:
<h1>{{ $name }}</h1>
If $name = "John", the page will show:
John
This syntax is:
- Safe
- Simple
- Very commonly used
Blade Syntax: If Condition (@if)
Blade allows conditions using @if.
Example:
@if($age >= 18)
<p>You are an adult</p>
@else
<p>You are under 18</p>
@endif
This works like normal PHP but looks cleaner.
Used for:
- Login checks
- Showing messages
- Conditional content
Blade Syntax: Looping Data (@foreach)
@foreach is used to loop data.
Example:
@foreach($users as $user)
<p>{{ $user }}</p>
@endforeach
This will display all users one by one.
Used for:
- Lists
- Tables
- Blog posts
- Products
Passing Data to Views
Data is passed from controller to view.
Controller example:
public function home()
{
$name = "Laravel User";
return view('home', compact('name'));
}
View file (home.blade.php):
<h1>Welcome {{ $name }}</h1>
Laravel automatically sends data to the view.
Summary
- Views show HTML pages
- Blade is Laravel’s template engine
{{ }}displays data@ifis used for conditions@foreachis used for loops- Data is passed from controller to view
Views and Blade make Laravel clean, powerful, and easy to use.
Category: Laravel
Tags: #Blade Templates
Rakesh
Thanks for sharing