为了账号安全,请及时绑定邮箱和手机立即绑定

当单个博客文章中的图像被删除时,如何减少图像顺序。

当单个博客文章中的图像被删除时,如何减少图像顺序。

PHP
MYYA 2023-07-01 15:14:05
如果特定博客文章中的图像已被删除,我想对图像顺序列进行递减,图像会递减,但没有获取帖子 ID。基本上,每篇博客文章的图像顺序都应从 1 开始,但它似乎会增加之前博客文章的顺序号。我尝试传递帖子 ID,但遇到了一些问题,感谢您的帮助,谢谢。public function destroy(Images $image)    {        $image->delete();        $image->update(['order' => 0]);        $images = Images::all();        $post = Post::all();        $i = 1;        foreach ($images as $img){            $img->timestamps = false;            $id = $img->id;            $img->update(['order' => $i])->where('post_id', $post->id);            $i++;        }        return Redirect::back()->with('message','Image Deleted!');    }
查看完整描述

1 回答

?
慕雪6442864

TA贡献1812条经验 获得超5个赞

如果您想更新某个帖子中的图像顺序,您将需要:


hasMany后模型中的关系:


 public function images()

{

    return $this->hasMany(Images::class);

}

重构delete函数:


public function destroy(Images $image)

{

   $postId = $image->post_id;


   $image->delete();


   $images = Post::find($postId)->images;


   $i = 1;


   foreach ($images as $img){

       $img->timestamps = false;


        $img->update(['order' => $i]);


      $i++;

   }


   return Redirect::back()->with('message','Image Deleted!');

}


查看完整回答
反对 回复 2023-07-01
  • 1 回答
  • 0 关注
  • 80 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信