第一个错误的代码:
@extends('layouts.app')
@section('content')
<h1> posts</h1>
@if(count($posts)>1)
@foreach($posts as $post)
<div class="well">
<h3>{{$post->title}}</h3>
</div>
@endforeach
@else
<p> no posts found</p>
@endif
@endsection
和图像也附上。在这里输入图像描述后,我按照铬的指示,这将发生和代码将是..就连我也删除了@ and try if(),就像这样。代码2;@extends('layouts.app')
@section('content')
<h1> posts</h1>
@if(count($posts ?? '')>1)
@foreach($posts ?? '' as $post)
<div class="well">
<h3>{{$post->title}}</h3>
</div>
@endforeach
@else
<p> no posts found</p>
@endif
@endsection
错误图像:
https://i.stack.imgur.com/21weC.png https://i.stack.imgur.com/RDdF2.png
发布于 2020-04-25 14:14:07
一种更简短的方法是:
@forelse($posts as $post)
<div class="well">
<h3>{{$post->title}}</h3>
</div>
@empty
<p> no posts found</p>
@endforelse
您可以在文档中找到更多循环:https://laravel.com/docs/7.x/blade#loops
发布于 2020-04-25 14:58:25
使用forelse而不是foreach,这应该可以完成工作。
https://stackoverflow.com/questions/61426923
复制