8. If 文・ループ文
@if (count($records) === 1)
I have one record!
@elseif (count($records) > 1)
I have multiple records!
@else
I don't have any records!
@endif
❖ @if, @elseif
最後の @endif を忘れないように注意
9. @unless (Auth::check())
You are not signed in. // ログインしてなければ表示
@endunless
❖ @unless
<title>
@hasSection('title')
@yield('title') - App Name
@else
App Name
@endif
</title>
❖ @hasSection
10. @for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
@endfor
@foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
@forelse ($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse
@while (true)
<p>I'm looping forever.</p>
@endwhile
❖ @for, @foreach, @forelse, @while
12. <html>
<head>
<title>App Name - @yield('title')</title>
</head>
<body>
@section('sidebar')
This is the master sidebar.
@show
<div class="container">
@yield('content')
</div>
</body>
</html>
master.blade.php
レイアウトの継承
❖ @yieldで子のブロックを出力する
❖ @sectionは親のブロックを子テンプレートに出力する
13. ❖ @extendsで親ページのビューを継承する
@extends('layouts.master')
@section('title', 'Page Title')
@section('sidebar')
@parent
<p>This is appended to the master sidebar.</p>
@endsection
@section('content')
<p>This is my body content.</p>
@endsection
呼び出すときは子テンプレートのビューを呼び出すだけで OK
Route::get('blade', function () {
return view('child');
});
child.blade.php