我在 Laravel 中有資料庫列images,其中包含如下影像串列["f3bd5ad57c8389a8a1a541a76be463bf.png","37aa5dfc44dddd0d19d4311e2c7a0240.jpg","e287f0b2e730059c55d97fa92649f4f2.jpg"] 現在用戶想要洗掉f3bd5ad57c8389a8a1a541a76be463bf.png我可以從路徑中洗掉影像,但我也想從陣列串列中洗掉這個影像。從我可以使用的路徑中洗掉
$path=$request->picloc;
$image_path = public_path($path);
if (file_exists($image_path)) {
File::delete($image_path);
// delete from database too
return response('file deleted');
}
我怎樣才能在 Laravel 中實作這一點?謝謝
uj5u.com熱心網友回復:
如果將影像轉換array為collection,則可以使用filter或reject方法洗掉所需的元素。
使用reject:
$images = [
"f3bd5ad57c8389a8a1a541a76be463bf.png",
"37aa5dfc44dddd0d19d4311e2c7a0240.jpg",
"e287f0b2e730059c55d97fa92649f4f2.jpg"
];
$imagesToRemove = ['f3bd5ad57c8389a8a1a541a76be463bf.png'];
$collection = collect($images)->reject(function ($value) use ($imagesToRemove) {
return in_array($value, $imagesToRemove);
});
dd($collection);
使用filter:
$images = [
"f3bd5ad57c8389a8a1a541a76be463bf.png",
"37aa5dfc44dddd0d19d4311e2c7a0240.jpg",
"e287f0b2e730059c55d97fa92649f4f2.jpg"
];
$imagesToRemove = ['f3bd5ad57c8389a8a1a541a76be463bf.png'];
$collection = collect($images)->filter(function ($value) use ($imagesToRemove) {
return !in_array($value, $imagesToRemove);
});
dd($collection);
兩者都達到相同的結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/505834.html
