Blade 에서 사용하는 유용한 문법
참조 : https://laravel.com/docs/10.x/blade
주석
{{-- 주석문 --}}
제어구조
@if ~ @else
@if(조건식)
@endif
@if(조건식)
@else
@endif
@if(조건식)
@elseif(조건식)
@endif
@switch
@switch($i)
@case(1)
First case...
@break
@case(2)
Second case...
@break
@default
Default case...
@endswitch
@for
@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
@endfor
@foreach
@foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
@foreach @break
@foreach ($users as $user)
@continue($user->type == 1)
<li>{{ $user->name }}</li>
@break($user->number == 5)
@endforeach
@forelse
@forelse ($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse
@while
@while (true)
<p>I'm looping forever.</p>
@endw
@unless
@unless (Auth::check())
You are not signed in.
@endunless
@isset
@isset($records)
// $records is defined and is not null...
@endisset
@empty
@empty($records)
// $records is "empty"...
@endempty
@production
// Production specific content...
@endproduction
@env('staging')
// The application is running in "staging"...
@endenv
@env(['staging', 'production'])
// The application is running in "staging" or "production"...
@endenv
@guest
@guest
// 로그인 전이면
@else
// 로그인 후이면
@endif
@guest('admin')
// The user is not authenticated...
@endguest
@auth
@auth
// The user is authenticated...
@endauth
@auth('admin')
// The user is authenticated... and have admin
@endauth
- Auth에 hasRole를 정의하여 role에 따라 다양한 디스플레이도 가능합니다.
- 아래처럼 사용하기 위해서는 hasRole이라는 trait을 사용해야 합니다. 이 파트는 Auth 에서 별도 설명 드리겠습니다.
@if (Auth::user()->hasRole('administrator'))
// admin일경우
@endif
@if (Auth::user()->hasRole('normal'))
// 일반 유저일 경우
@endif
@php
- blade에서 php 문법을 사용하여 바로 코딩을 할때 유용합니다.
@php
// php 문법
@endphp
@hasSection('navigation')
<div class="pull-right">
@yield('navigation')
</div>
<div class="clearfix"></div>
@endif
@sectionMissing('navigation')
<div class="pull-right">
@include('default-navigation')
</div>
@endif
Form 관련 Attributes
@checked
<input type="checkbox"
name="active"
value="active"
@checked(old('active', $user->active)) />
@selected
<select name="version">
@foreach ($product->versions as $version)
<option value="{{ $version }}" @selected(old('version') == $version)>
{{ $version }}
</option>
@endforeach
</select>
@disabled
<button type="submit" @disabled($errors->isNotEmpty())>Submit</button>
@readonly
<input type="email"
name="email"
value="[email protected]"
@readonly($user->isNotAdmin()) />
@required
<input type="text"
name="title"
value="title"
@required($user->isAdmin()) />
Css 관련 Style 및 Class
@class
<span @class([
'p-4',
'font-bold' => $isActive,
'text-gray-500' => ! $isActive,
'bg-red' => $hasError,
])></span>
@style
<span @style([
'background-color: red',
'font-weight: bold' => $isActive,
])></span>
Form 관련
CSRF Field
<form method="POST" action="/profile">
@csrf
...
</form>
Method Field
<form action="/foo/bar" method="POST">
@method('PUT')\
...
</form>
Validation Errors
<label for="title">Post Title</label>
<input id="title"
type="text"
class="@error('title') is-invalid @enderror">
@error('title')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
@include
- php의 include 와 동일한 기능을 제공합니다.
@include('view.name')
@include('view.name', [전송할 변수] )
@include('view.name', ['page'=>$page] )
@includeIf
@includeIf('view.name', ['status' => 'complete'])
@each
@each('view.name', $jobs, 'job')