LARAVEL

Larvel View


In Laravel, views are used to render the HTML markup and presentation layer of your web application. Views are typically created using the Blade templating engine, which provides a concise and expressive syntax for writing templates. Here's a more detailed look at working with views in Laravel

 

1. Creating Views

Views in Laravel are stored in the resources/views directory. You can create a new view file by adding a Blade template file with the .blade.php extension. For example, create a view file named welcome.blade.php:

<!-- resources/views/welcome.blade.php -->

<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <h1>Welcome to my Laravel application!</h1>
</body>
</html>

 

2. Loading Views from Controllers

You can load a view from a controller method using the view() helper function. In your controller method, simply return the name of the view you want to load:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class WelcomeController extends Controller
{
    public function index()
    {
        return view('welcome');
    }
}

 

3. Passing Data to Views

You can pass data to your views by passing an associative array as the second argument to the view() function. The array keys will be the variable names accessible in your view:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class WelcomeController extends Controller
{
    public function index()
    {
        $name = 'John Doe';
        return view('welcome', ['name' => $name]);
    }
}

You can then access the $name variable in your view file:

<!-- resources/views/welcome.blade.php -->

<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <h1>Welcome, {{ $name }}!</h1>
</body>
</html>

 

5. Blade Templating Engine

Blade is Laravel's powerful templating engine, which allows you to use features like template inheritance, control structures, and directives. Here are a few examples:

i) Template Inheritance

Use the @extends directive to define a master layout and @yield to specify content sections:

<!-- resources/views/layouts/master.blade.php -->

<!DOCTYPE html>
<html>
<head>
    <title>@yield('title', 'Default Title')</title>
</head>
<body>
    <div class="container">
        @yield('content')
    </div>
</body>
</html>
<!-- resources/views/welcome.blade.php -->

@extends('layouts.master')

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

@section('content')
    <h1>Welcome to my Laravel application!</h1>
@endsection

 

ii) Control Structures

Use @if, @else, @foreach, and other control structures directly in your Blade templates:

<!-- resources/views/profile.blade.php -->

@if($user->isAdmin)
    <p>Welcome, Admin!</p>
@else
    <p>Welcome, User!</p>
@endif

These are just some of the basics of working with views and Blade in Laravel. Laravel's documentation provides a comprehensive guide to views and Blade templating if you want to explore more advanced features: Laravel - Blade Templates.


LARAVEL