ViewController 里的数据
class ViewController extends Controller
{
public function index()
{
$data = [
'score' => 50,
'num' => 10,
'article' => [
'news1',
'news2',
'news3',
'news4',
'news5',
],
'news' => [],
];
return view('my_laravel', ['data' => $data]);
}
}
.blade.php 代码
1 @if
@if ($data['score'] < 60)
failed
@else
pass
@endif
2 @unless
@unless ($data['score'] > 60)
Sorry, you failed.
@endunless
3 @for
@for ($i = 0; $i <= $data['num']; $i++)
{{$i}}
@endfor
4 @foreach and @forelse
@foreach ($data['article'] as $v)
{{$v}}
@endforeach
</br>
@forelse ($data['news'] as $v)
{{$v}}
@empty
no data;
@endforelse
5 嵌套
@foreach ($data['article'] as $k => $v)
@if ($k > 2)
{{$k}} => {{$v}}
@endif
@endforeach