如果我的查询返回包含3个帖子,其中id为1,2和3的 $posts ,我可以以某种方式删除id为2的 $post ,这样当我循环 $posts 时,我只输出id为1和3的帖子吗?
$posts
$post
使用刀片:
@foreach($posts as $post) @if(!$post->id == 2 ) {{$post->title}} @endif @endforeach
在获取$ posts后,您可以过滤
$posts = $posts->filter(function ($value, $key) { return $value->id != 2; });
看到这个:
https://laravel.com/docs/5.7/collections#method-filter
2 回答
使用刀片:
在获取$ posts后,您可以过滤
看到这个:
https://laravel.com/docs/5.7/collections#method-filter