亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當單個博客文章中的圖像被刪除時,如何減少圖像順序。

當單個博客文章中的圖像被刪除時,如何減少圖像順序。

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 關注
  • 163 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號