Lesson 11 of 50 – Passing Data to Views
22%

Laravel Views and Passing Data

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.

1. What Is a View in Laravel?

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.

2. Create Your First Blade View

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.

3. Pass a Single Variable to a View

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:

Student Name: Rahul

The array key name becomes the $name variable in the view.

4. Pass Multiple Variables to a 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.

5. Use compact() to Pass Variables

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.

6. Display Arrays in a Blade 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:

  • PHP
  • Laravel
  • JavaScript
  • React

Blade repeats the list item for every course in the array.

7. Display Conditional Content in Views

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.

8. Pass Data from a Controller to a View

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.

9. Practical Assignment

Practise passing data from routes and controllers to Blade views.

  1. Create a home.blade.php view.
  2. Display a student name using a Blade variable.
  3. Pass a course name and fees to a view.
  4. Use compact() to pass three variables.
  5. Display an array of five course names using @foreach.
  6. Use @if to display Pass or Fail based on marks.
  7. Create a controller that passes student names to a view.
  8. Use @forelse to display a message when the list is empty.
  9. Test each page in your browser.

Remember: keep application logic in controllers and presentation code in Blade views whenever practical.


🧠 Quick Quiz

1. Where are Laravel Blade views usually stored?




2. Which helper returns a Laravel view?




3. Which Blade directive loops through an array?