Views display the user interface of a Laravel application. Laravel uses Blade templates to create dynamic HTML pages. Controllers can pass data to views so that information from your application can be displayed on the page.
A view contains the HTML and presentation code displayed to the user.
Laravel views are usually stored in:
resources/views/
For example:
resources/views/home.blade.php
The .blade.php extension indicates that
the file uses Laravel's Blade templating engine.
Views help separate presentation from application logic, making your code easier to maintain.
Create a file named
home.blade.php inside
resources/views/.
File: home.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Welcome to Laravel</h1>
<p>Learn programming with Soopro Pathshala.</p>
</body>
</html>
Now register a route in
routes/web.php.
use Illuminate\Support\Facades\Route;
Route::get('/home', function () {
return view('home');
});
Start your development server:
php artisan serve
Visit http://127.0.0.1:8000/home
to display the page.
You can pass a variable from a route or controller to a view using an associative array.
Route in routes/web.php:
Route::get('/student', function () {
$name = 'Rahul';
return view('student', ['name' => $name]);
});
Create resources/views/student.blade.php.
<h1>Student Details</h1>
<p>Student Name: {{ $name }}</p>
Output:
The array key name becomes the
$name variable in the view.
You can pass multiple variables to a view in one associative array.
Route::get('/student-details', function () {
$name = 'Amit';
$course = 'Laravel';
$fees = 10000;
return view('student-details', [
'name' => $name,
'course' => $course,
'fees' => $fees
]);
});
In resources/views/student-details.blade.php:
<h1>Student Details</h1>
<p>Name: {{ $name }}</p>
<p>Course: {{ $course }}</p>
<p>Course Fees: ₹{{ $fees }}</p>
Blade displays each variable in its corresponding location.
PHP's compact() function creates an
associative array from existing variable names.
Example in a controller:
public function index()
{
$name = 'Priya';
$course = 'PHP';
$duration = '6 Months';
return view('student', compact(
'name',
'course',
'duration'
));
}
In the Blade view, access the variables as usual:
<h2>{{ $name }}</h2>
<p>Course: {{ $course }}</p>
<p>Duration: {{ $duration }}</p>
compact() is convenient when several
existing PHP variables need to be passed to a view.
You can pass an array to a view and display its
contents using Blade's @foreach directive.
Route:
Route::get('/courses', function () {
$courses = [
'PHP',
'Laravel',
'JavaScript',
'React'
];
return view('courses', compact('courses'));
});
File: courses.blade.php
<h1>Available Courses</h1>
<ul>
@foreach ($courses as $course)
<li>{{ $course }}</li>
@endforeach
</ul>
Output:
Blade repeats the list item for every course in the array.
Blade's @if directive displays content
depending on a condition.
Route:
Route::get('/result', function () {
$marks = 85;
return view('result', compact('marks'));
});
File: result.blade.php
<h1>Exam Result</h1>
<p>Marks: {{ $marks }}</p>
@if ($marks >= 40)
<p>Result: Pass</p>
@else
<p>Result: Fail</p>
@endif
If the marks are 40 or above, the page displays Pass. Otherwise, it displays Fail.
You can also use @elseif when multiple
conditions need to be checked.
In a real Laravel application, controllers usually prepare the data and return the view.
Inside StudentController.php:
<?php
namespace App\Http\Controllers;
class StudentController extends Controller
{
public function index()
{
$students = [
'Rahul',
'Priya',
'Amit'
];
return view('students', compact('students'));
}
}
Register the route in routes/web.php:
use App\Http\Controllers\StudentController;
Route::get('/students', [StudentController::class, 'index']);
File: students.blade.php
<h1>Student List</h1>
<ul>
@forelse ($students as $student)
<li>{{ $student }}</li>
@empty
<li>No students found.</li>
@endforelse
</ul>
The @forelse directive displays the
list when data exists and the empty message when
the array has no items.
Practise passing data from routes and controllers to Blade views.
home.blade.php view.compact() to pass three variables.@foreach.@if to display Pass or Fail based on marks.@forelse to display a message when the list is empty.Remember: keep application logic in controllers and presentation code in Blade views whenever practical.
1. Where are Laravel Blade views usually stored?
2. Which helper returns a Laravel view?
3. Which Blade directive loops through an array?