如果特定博客文章中的圖像已被刪除,我想對圖像順序列進(jìn)行遞減,圖像會遞減,但沒有獲取帖子 ID?;旧?,每篇博客文章的圖像順序都應(yīng)從 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貢獻(xiàn)1812條經(jīng)驗 獲得超5個贊
如果您想更新某個帖子中的圖像順序,您將需要:
hasMany后模型中的關(guān)系:
public function images()
{
return $this->hasMany(Images::class);
}
重構(gòu)delete函數(shù):
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!');
}
- 1 回答
- 0 關(guān)注
- 149 瀏覽
添加回答
舉報
0/150
提交
取消