LoginSignup
0
0

Laravelのディレクティブまとめ

Posted at

はじめに

Laravelを勉強中の初心者です。この記事では、Laravelでよく使われるディレクティブについてまとめていきます。
参照:https://readouble.com/laravel/9.x/ja/blade.html

1.レイアウトに関連するディレクティブ

1-1.ビューの継承(@￰extends)

他のビューを継承する。

@extends('layouts.app')

1-2.ビューの挿入(@￰include)

他のビューファイルを読み込んで表示する。

@include('sidebar')

1-3.セクションの挿入(@￰section,@￰yield)

@￰sectionでセクションの内容を定義し、@￰yieldで内容を表示する。

@section('content')
   <p>コンテンツ</p>
@endsection
@yield('content')

2.処理に関連するディレクティブ

2-1.条件分岐(@￰if)

@if (Auth::check())
    <p>ログインしています。</p>
@else
    <p>ログインしていません。</p>
@endif

2-2.ループ(@￰for,@￰foreach,@￰while)

1から5までの数字を出力する。

@for ($i = 1; $i <= 5; $i++)
    <li>{{ $i }}</li>
@endfor
@php
$numbers = [1, 2, 3, 4, 5];
@endphp

@foreach ($numbers as $number)
    <p>{{ $number }}</p>
@endforeach
@php
$i = 1;
@endphp

@while ($i <= 5)
    <p>{{ $i }}</p>
    @php
    $i++;
    @endphp
@endwhile

2-3.CSRF対策(@￰csrf)

フォームでセキュリティを保護するためにCSRFトークンを生成する(下記)。

@csrf

2-4.HTMLメソッド(@￰method)

PUT, PATCH, DELETEなどのHTTPメソッドを直接使えないので、@￰methodを使う。

<form action="/hoge" method="POST">
    @method('PUT')
    @csrf
</form>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0