book cover front cover for API Development with Laravel by Akintoye Adegoke

🚀 It’s here!
After months of writing and testing real-world code examples, I’m excited to announce my new book — API Development with Laravel: A Quick Start Guide 🎉
This project-based book introduces you to Laravel, even if you’re completely new to it — and shows you how to build a real Payment Processor API step by step.

🖼️ Laravel Views: A Beginner’s Guide

Laravel is one of the most popular PHP frameworks, and one of the things that makes it great is how it separates logic (what your app does) from presentation (what your users see). This is where Views come in.

In this article, we’ll explain what Views are in Laravel, why they’re useful, and how to use them with simple examples.


🤔 What is a View?

A View in Laravel is simply a file that contains HTML (and sometimes a bit of PHP) to display content to the user. Think of it like the front-end of your application — the part people actually see.

Laravel stores its view files in the resources/views folder.


📁 Where Are Views Stored?

By default, all your view files are stored here:

 
resources/views/

Each view is usually a .blade.php file, because Laravel uses a templating engine called Blade.

For example:

 
resources/views/welcome.blade.php

📄 Creating a Simple View

Let’s say you want to create a page called "hello.blade.php".

  1. Go to resources/views/

  2. Create a file named hello.blade.php

  3. Add some HTML:

 
<!-- resources/views/hello.blade.php -->
<!DOCTYPE html>
<html>
<head>
    <title>Hello Page</title>
</head>
<body>
    <h1>Hello, Laravel!</h1>
</body>
</html>

🚀 Returning a View from a Route

Now that you have a view, how do you show it in your app?

Open the routes/web.php file and add this:

 
Route::get('/hello', function () {
    return view('hello');
});

Notice that we use view('hello') — Laravel automatically looks for a file named hello.blade.php in the resources/views folder.


🧠 Passing Data to a View

You can also pass data from your route to your view. Here's how:

Step 1: Update your route

 
Route::get('/greet', function () {
    $name = 'John';
    return view('greet', ['name' => $name]);
});

Step 2: Create the greet.blade.php file

 
<!-- resources/views/greet.blade.php -->
<h1>Hello, {{ $name }}!</h1>

Now, when you visit /greet, it will display:
Hello, John!


🧩 Blade Templating: Making Views Dynamic

Blade lets you use simple, readable syntax for things like:

Echoing data:

 
{{ $name }}

If statements:

 
@if($name == 'John')
    <p>Welcome back, John!</p>
@else
    <p>Hello, stranger!</p>
@endif

Loops:

 
@foreach($users as $user)
    <p>{{ $user }}</p>
@endforeach

🧱 View Layouts with @yield and @section

If you have a website with a consistent layout (like a navbar and footer), you can use layouts.

1. Create a layout file:

 
<!-- resources/views/layouts/main.blade.php -->
<html>
<head>
    <title>@yield('title')</title>
</head>
<body>
    <div class="content">
        @yield('content')
    </div>
</body>
</html>

2. Create a view that uses the layout:

 
<!-- resources/views/about.blade.php -->
@extends('layouts.main')

@section('title', 'About Page')

@section('content')
    <h1>About Us</h1>
    <p>This is the about page.</p>
@endsection

This makes your code cleaner and easier to manage.


🧼 Summary

  • Views are used to display HTML to users.

  • Stored in resources/views.

  • Use the view() function to return them from routes.

  • Use Blade syntax ({{ }}, @if, @foreach, etc.) for cleaner templates.

  • Layouts help you reuse the same structure across pages.


✅ Final Tip

Whenever you’re working with Laravel Views, remember:

  • Keep your logic in controllers,

  • Keep your HTML in views,

  • And keep your app clean and organized!

Laravel views + Blade templating make building websites fast, elegant, and fun.

Comments