LoginSignup
0
0

More than 3 years have passed since last update.

laravel ディレクティブについてのまとめ

Posted at

laravel ディレクティブについて

bladeテンプレート(view)の中で利用できる命令のこと

○○.php
@foreach(連想配列as変数)
//PHPのforeachと同じ
@if条件式
//phpのifと同じ
@unless(変数)
//falseの場合に出力
@iseet(変数)
//trueの場合に出力
@empty(変数)
//変数が存在しない場合のみ(null)の時に出力
@switch
//phpのswitchと同じ
@for
//phpのfor文と同じ
@break
//phpのbreak文と同じ
@loop
//ちょっと特殊 ループ変数と呼ばれる ※わかりづらいので下記に例あり
//index:インデックス番号(0スタート)
//iteration:繰り返し数(1スタート)
//remaining:残っている要素数
//count:配列の総数
//first:最初の項目->>>配列の先頭の時には1を返す
//last:最後の項目 ->>>配列の最後尾の時には1を返す
//even:偶数回目か
//odd:奇数回目か
//depth:入れ子レベル
//parent:親のループ変数(ネストした入れ子の場合)
@forelse
//処理
@empty 
//配列が空の場合の処理
//配列が空の場合の出力を設定できる
//※データの表示によって振り分けるのに@ifディレクティブよりも@forelseの方がおすすめ
@php
//<?phpと同じだが使用は例外的とのこと
@yield(name,[,default])
//name テンプレートからプレイスホルダー(空)の変数を受け取る時に使用

//①
@section(name)
//default
 @show
//②
@section()
@parent
//default
//名前が示す通りにコンテンツのセクションを定義※上記の様に複数の定義方法がある

//default-> 規程のコンテンツ(アクション変数とか)
@each(path,データ,仮変数,配列が空の時に実行)


※※loopディレクティブの例

controller.php
    public function foreach_loop()
    {
        return view('view.foreach_loop', [
            'weeks' => [ '月', '火', '水', '木', '金', '土', '日' ]
        ]);

    }

foreach_loop.php
<tr>
  <th></th>
  <th>index</th>
  <th>iteration</th>
  <th>count</th>
  <th>first</th>
  <th>last</th>
  <th>even</th>
  <th>odd</th>
  <th>depth</th>
</tr>
@foreach ($weeks as $week)
  <tr>
    <td>{{ $week }}</td>
    <td>{{ $loop->index }}</td>
    <td>{{ $loop->iteration }}</td>
    <td>{{ $loop->count }}</td>
    <td>{{ $loop->first }}</td>
    <td>{{ $loop->last }}</td>
    <td>{{ $loop->even }}</td>
    <td>{{ $loop->odd }}</td>
    <td>{{ $loop->depth }}</td>
  </tr>
@endforeach
</body>
</html>

スクリーンショット 2020-10-08 19.12.28.png

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