yeaminraat/laravel-trash
Composer 安装命令:
composer require yeaminraat/laravel-trash
包简介
A Laravel package to store deleted model data and restore records safely.
README 文档
README
A Laravel package that provides a comprehensive trash/soft-delete system with restore capabilities. When models are deleted, they are automatically moved to a trash bin where they can be viewed, restored, or permanently deleted.
Features
- 🗑️ Automatic Trash Management: Automatically saves deleted models to a trash table
- 🔄 Easy Restore: Restore deleted items with a single click
- 👁️ View Trash Items: Browse all trashed items through a web interface
- 🗂️ Model Agnostic: Works with any Eloquent model
- 🎨 Beautiful UI: Modern, responsive interface built with Tailwind CSS
- 🔒 Safe Deletion: Permanent deletion requires explicit action
Installation
Step 1: Install via Composer
composer require yeaminraat/laravel-trash
Step 2: Publish Migrations
php artisan vendor:publish --tag=laravel-trash-migrations
Step 3: Run Migrations
php artisan migrate
Step 4: (Optional) Publish Views
If you want to customize the trash views:
php artisan vendor:publish --tag=laravel-trash-views
Usage
Making Models Trashable
Add the Trashable trait to any Eloquent model you want to track:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use YeaminRaat\LaravelTrash\Traits\Trashable; class Post extends Model { use Trashable; protected $fillable = [ 'title', 'content', 'author_id', ]; }
Deleting Models
When you delete a model that uses the Trashable trait, it will automatically be saved to the trash table:
$post = Post::find(1); $post->delete(); // Automatically saved to trash
Restoring Models
You can restore a model from trash using the restoreFromTrash() method:
$post = Post::find(1); $result = $post->restoreFromTrash(); if ($result['status'] === 'success') { echo $result['message']; // "Restored successfully" } else { echo $result['message']; // Error message }
Accessing the Trash Interface
The package provides a web interface to manage trash items. Access it at:
http://your-app.test/trash
Available Routes:
GET /trash- View all trash itemsGET /trash/{id}- View details of a specific trash itemPOST /trash/restore/{id}- Restore a trash itemDELETE /trash/delete/{id}- Permanently delete a trash item
Programmatic Trash Management
You can also manage trash programmatically:
use YeaminRaat\LaravelTrash\Models\Trash; // Get all trash items $trashItems = Trash::latest()->get(); // Get trash for a specific model $postTrash = Trash::where('model_type', Post::class)->get(); // Restore via controller logic $trashItem = Trash::find(1); $model = new $trashItem->model_type(); $record = $model->find($trashItem->model_id); if ($record) { $record->update($trashItem->data); } else { $model->create($trashItem->data); } $trashItem->delete();
How It Works
-
Deletion: When a model using the
Trashabletrait is deleted, thebootTrashable()method intercepts the deletion event and saves the model's data to thetrashtable. -
Storage: The trash table stores:
model_type: The fully qualified class name of the deleted modelmodel_id: The ID of the deleted modeldata: JSON representation of the model's attributescreated_at: When the item was trashed
-
Restoration: When restoring, the package:
- Retrieves the trashed data
- Either updates the existing model (if it still exists) or creates a new one
- Removes the item from the trash table
Database Schema
The package creates a trash table with the following structure:
Schema::create('trash', function (Blueprint $table) { $table->id(); $table->string('model_type'); $table->unsignedBigInteger('model_id'); $table->json('data'); $table->timestamps(); });
Error Handling
The package handles common errors during restoration:
- Unique Constraint Violations: If restoring would violate unique constraints, an error message is returned
- General Exceptions: Any unexpected errors are caught and reported
Requirements
- PHP >= 8.0
- Laravel >= 8.0
Author
Md Eamin Hossain
- Email: yeaminhossain2@gmail.com
License
This package is open-sourced software. Please check the license file for more details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
If you encounter any issues or have questions, please open an issue on the GitHub repository.
yeaminraat/laravel-trash 适用场景与选型建议
yeaminraat/laravel-trash 是一款 基于 Blade 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Audit」 「laravel」 「restore」 「trash」 「soft-delete」 「delete-log」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yeaminraat/laravel-trash 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yeaminraat/laravel-trash 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yeaminraat/laravel-trash 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Log adding/updating/deleting of elements
Doctrine ORM provider for the auditor audit-log library.
PB Web Media Audit Bundle for Symfony
laravel package to backup/restore files and database.
Database Backup and Restore functionality
An easy way backup and restore databases in Laravel.
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-07