Laravel Basic
Laravel Form
Laravel Database
Laravel Advance
resources/views
directory..blade.php
files).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:
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:
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:
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:
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>
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:
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:
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:
<script>
).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:
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:
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:
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: