mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-25 13:44:06 -08:00
Use the observer to delete associated files and detach users
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
d4a66f9b6c
commit
c2fe3b5459
|
@ -182,7 +182,7 @@ class ConsumablesController extends Controller
|
||||||
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
|
return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.not_found'));
|
||||||
}
|
}
|
||||||
$this->authorize($consumable);
|
$this->authorize($consumable);
|
||||||
$consumable->users()->detach();
|
|
||||||
$consumable->delete();
|
$consumable->delete();
|
||||||
// Redirect to the locations management page
|
// Redirect to the locations management page
|
||||||
return redirect()->route('consumables.index')->with('success', trans('admin/consumables/message.delete.success'));
|
return redirect()->route('consumables.index')->with('success', trans('admin/consumables/message.delete.success'));
|
||||||
|
|
|
@ -5,6 +5,8 @@ namespace App\Observers;
|
||||||
use App\Models\Actionlog;
|
use App\Models\Actionlog;
|
||||||
use App\Models\Consumable;
|
use App\Models\Consumable;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
class ConsumableObserver
|
class ConsumableObserver
|
||||||
{
|
{
|
||||||
|
@ -66,6 +68,28 @@ class ConsumableObserver
|
||||||
*/
|
*/
|
||||||
public function deleting(Consumable $consumable)
|
public function deleting(Consumable $consumable)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
$consumable->users()->detach();
|
||||||
|
|
||||||
|
foreach ($consumable->uploads() as $file) {
|
||||||
|
try {
|
||||||
|
Storage::disk('public')->delete('consumables/'.$file);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::info($e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Storage::disk('public')->delete('consumables/'.$consumable->image);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::info($e);
|
||||||
|
}
|
||||||
|
|
||||||
|
$consumable->image = null;
|
||||||
|
$consumable->save();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$logAction = new Actionlog();
|
$logAction = new Actionlog();
|
||||||
$logAction->item_type = Consumable::class;
|
$logAction->item_type = Consumable::class;
|
||||||
$logAction->item_id = $consumable->id;
|
$logAction->item_id = $consumable->id;
|
||||||
|
|
Loading…
Reference in a new issue