0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Bladeでのループ処理/分岐処理

Posted at

// resources/views/index.blade.php

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>My BBS</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <div class="container">
        <h1>My BBS</h1>
        <ul>
/*
1.@foreach(配列 as 変数) ~ @endforeach
最後の@endforeachの後には ; がいらないので注意する。
このような@で始まる便利な構文をディレクティブと呼びます。
*/
            @foreach ($posts as $post)
                <li>{{ $post }}</li>
            @endforeach
        </ul>
        <ul>

/*
2. @forelse ~ @empty ~ @endforelse で配列が空の場合の処理を分岐することもできる。
@empty 以降で配列が空の場合の処理を記述し、@endforelseで締める。
*/
            @forelse ($posts as $post)
                <li>{{ $post }}</li>
            @empty
                <li>No posts yet!</li>
            @endforelse
        </ul>
    </div>
</body>
</html>
// routes/web.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    $posts = [
	// 3. 以下のように配列が空になっている。
        // 'TitleA',
        // 'TitleB',
        // 'TitleC',
    ];

    return view('index')
        ->with(['posts' => $posts]);
});

分岐処理(if文)

/*
if文も文頭に@をつけることは変わらないが、{ }は必要ないことに注意する。
また、条件式の値を{{ }}で実体参照する必要はない。(文法エラーになってしまう。)
*/
@foreach($members as $member)
	@if ($mondayShift->morning1 === $member->name)
		<p>{{ $member->name }}</p>
	@else
		<p>名前が違います</p>
	@endif
@endforeach
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?