LoginSignup
5

More than 3 years have passed since last update.

【概要】

1.値

2.条件分岐ディレクティブ

3.繰り返しディレクティブ

補足(i)

補足(ii)

1.値

{{***}}

{{}}の中身は値だけでなく、変数・関数なども使用できます。HTML文でエスケープ処理されたくない時は、下記のように使用します。

{{!! !!}}


2.条件分岐ディレクティブ

@if (条件式)
//内容
@elseif (条件式)
//内容
@else
//内容
@endif

また@ifの逆の@unless-@endunlessもあります。


3.繰り返しディレクティブ

for

@for(初期値 ; 条件 ; 処理) #例:@for($i=0 ; $i<=10 ; $i++)
//内容 #例:echo $i;
@endfor

また@breakは繰り返し処理の中断、@continueはcontinue以降は表示せず@forの繰り返し処理を引き続き行います。

foreach

@foreach ($配列 as $変数)
//内容
@empty #--❶
//変数が空の時の内容
@endforeach

❶については記載しなくてもOKです。@ifでいう@elseにあたります。

while

@while (条件)
//内容
@endwhile


補足(i)

3.の繰り返しディレクティブには、$loopというループ変数があり、繰り返し処理のプロパティとして使用することが可能です。

@foreach($category as $item)
 @if ($loop->first) #---❶
  <h1>アイテム一覧</h1>
  <ul>
 @endif
  <li>No.{{$loop->iteration}}.{{$item}}</li> #---❷
 @if ($loop->last)  #---❸
  </ul>
 @endif
@endforeach

❶:loop->firstは、最初の繰り返しならtrueを返します。
❷:loop->iterationは、1から順番に繰り返し、その回数を返します。
❸:loop->lastは、最後の繰り返しならtrueを返します。


補足(ii)

index.phpファイルとindex.blade.phpファイルが同じviewフォルダの中の指定のフォルダに入っていたとします。その時に、controllerにある”return view('@@@@.index' , $@@@@)'はLaravelでは'index'としてした場合はindex.blade.phpファイルが優先して読み込まれます。

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
5