Laravel View

What is a View in Laravel?

  • In Laravel, Views are used to separate presentation logic (HTML, CSS, JS) from business logic (controllers, models).
  • Views are stored in the resources/views directory.
  • Views are usually written in Blade Templating Engine (.blade.php files).

Basic View Route

Map a route to a view method:

web.php(routes\web.php):

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


welcome.blade.php (resources\views\welcome.blade.php)

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Laravel</title>
    </head>
    <body>
    </body>
        <h1>Welcome to Laravel</h1>
        <p>This is a simple welcome page.</p>
    </body>
</html>


Output:


Return a View

web.php(routes\web.php):

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


welcome.blade.php (resources\views\welcome.blade.php)

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Laravel</title>
    </head>
    <body>
    </body>
        <h1>Hello, {{ $name }}</h1>
    </body>
</html>


Output:


Multiple Data

web.php(routes\web.php):

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


welcome.blade.php (resources\views\welcome.blade.php)

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Laravel</title>
    </head>
    <body>
    </body>
        <h1>Hello, {{ $name }}</h1>
        <div>age, {{ $age}}</h1>
    </body>
</html>


Output:


Nested Views (Folder Structure)

web.php(routes\web.php):

Route::get('/profile', function () {
  return view('users.profile');
});


profile.blade.php (resources\views\users\profile.blade.php)

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Laravel</title>
    </head>
    <body>
    </body>
        <h1>Profile page</h1>
    </body>
</html>


Output:


Global Data for All Views

App\Providers\AppServiceProvider.php

use Illuminate\Support\Facades\View;

public function boot()
{
  View::share('appName', 'My Laravel App');
}


Now available in any Blade file:

<p>{{ $appName }}</p>

Blade Directives

@if, @elseif, @else, @endif

web.php(routes\web.php):

Route::get('/welcome', function () {
  return view('welcome',['age' => 18]);
});


welcome.blade.php (resources\views\welcome.blade.php)

@if($age > 30 && $age < 50)
  <p>Senior</p>
@elseif($age >= 18)
  <p>Major</p>
@else
  <p>Minor</p>
@endif


Output:

@isset, @empty, @unless

web.php(routes\web.php):

Route::get('/welcome', function () {
  return view('welcome',['records' => [],'name' => 'John','loggedIn' => false]);
});


welcome.blade.php (resources\views\welcome.blade.php)

@isset($name)
    <p>{{ $name }}</p>
@endisset
 
@empty($records)
    <p>No records found.</p>
@endempty
 
@unless($loggedIn)
    <p>Please log in.</p>
@endunless


Output:

@foreach, @for

web.php(routes\web.php):

Route::get('/welcome', function () {
    return view('welcome',[
        'users' => [
            ['name' => 'John'],
            ['name' => 'Jane'],
            ['name' => 'Doe']
        ]
    ]);
});


welcome.blade.php (resources\views\welcome.blade.php)

@foreach($users as $user)
  <p>{{ $user['name'] }}</p>
@endforeach

@for($i = 0; $i < 10; $i++)
    {{ $i }}
@endfor


Output:


Outputting Data

Escaped Output (Safe)

  • Laravel escapes HTML special characters.
  • Prevents XSS attacks (someone inserting <script>).
  • Best for normal text.


web.php(routes\web.php):

Route::get('/welcome', function () {
  return view('welcome', ['title' => '<b>Hello World</b>']);
});


welcome.blade.php (resources\views\welcome.blade.php)

<h1>{{ $title }}</h1>


Output:

Unescaped Output (Dangerous if user input)

  • Laravel outputs raw HTML.
  • Useful if you want HTML rendered as HTML.
  • ⚠️ Dangerous if data comes from users (possible XSS).


web.php(routes\web.php):

Route::get('/welcome', function () {
  return view('welcome', ['title' => '<b>Hello World</b>']);
});


welcome.blade.php (resources\views\welcome.blade.php)

<h1>{!! $title !!}</h1>


Output:


Including Views (Reusable Components)

web.php(routes\web.php):

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


welcome.blade.php (resources\views\welcome.blade.php)

@include('partials.header')
<h1>Main Content</h1>
@include('partials.footer')


header.blade.php (resources\views\partials\header.blade.php)

<h1>Header</h1>


footer.blade.php (resources\views\partials\footer.blade.php)

<h1>Footer</h1>


Output:


Layouts & Sections (Master Layout)

app.blade.php(resources/views/layouts/app.blade.php)

<!DOCTYPE html>
<html>
  <body>
    <div>@yield('title')</div>
    <div class="container">
      @yield('content')
    </div>
  </body>
</html>



welcome.blade.php (resources\views\welcome.blade.php)

@extends('layouts.app')

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

@section('content')
  <h1>Welcome to Home Page</h1>
@endsection


Output:


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.