首页 文章

如果我知道$ post的ID,我可以从$ posts中删除特定的$ post吗?

提问于
浏览
0

如果我的查询返回包含3个帖子,其中id为1,2和3的 $posts ,我可以以某种方式删除id为2的 $post ,这样当我循环 $posts 时,我只输出id为1和3的帖子吗?

2 回答

  • -1

    使用刀片:

    @foreach($posts as $post)
       @if(!$post->id == 2 )
         {{$post->title}}
       @endif
    @endforeach
    
  • 2

    在获取$ posts后,您可以过滤

    $posts = $posts->filter(function ($value, $key) {
        return $value->id != 2;
    });
    

    看到这个:

    https://laravel.com/docs/5.7/collections#method-filter

相关问题