cesargb / laravel-model-toolkit
List all models, detect and delete orphaned morph relation records, and prune Eloquent models.
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/cesargb/laravel-model-toolkit
Requires
- php: ^8.4
- illuminate/console: ^12.0|^13.0
- illuminate/database: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- symfony/finder: ^7.0
Requires (Dev)
- larastan/larastan: ^3.0
- orchestra/testbench: ^10.0|^11.0
- phpunit/phpunit: ^12.5
README
Laravel Model Toolkit
A Laravel package that automatically discovers all Eloquent models, detects orphaned records in polymorphic relationships, and removes unwanted entries. It also executes cleanup for prunable models in a single pass.
What Does This Package Do?
When you delete a parent model (for example, a Post), related tables
using polymorphic relationships (such as images, comments, or
taggables) may retain records pointing to IDs that no longer exist.
These are considered orphaned records.
This package:
- ✅ Automatically scans all Eloquent models in your application
- ✅ Detects and removes orphaned records in polymorphic relationships
- ✅ Detects and executes cleanup for models using Laravel's
PrunableorMassPrunabletraits in the same command
Requirements
- PHP 8.4 or higher
- Laravel 12 or higher
Installation
Install the package via Composer:
composer require cesargb/laravel-model-toolkit
Available Commands
model:clean
Displays orphaned records and prunable models, then removes them.
php artisan model:clean
Options
--pretend; Preview what would be deleted without actually deleting records
--chunk=1000; Number of records processed per batch during deletion
model:list
Lists all discovered Eloquent models grouped by namespace along with their database tables.
php artisan model:list
Options
--json; Output results in JSON format
Programmatic Usage
You can also use the internal classes directly in your application code.
Retrieve All Polymorphic Relationships and Count Orphans
use Cesargb\ModelToolkit\Morph; $morph = new Morph(); $models = $morph->get();
Clean Orphans for a Specific Model and Relation
$deleted = $morph->clean(\App\Models\Post::class, 'comments');
List Prunable Models
use Cesargb\ModelToolkit\Prunable; $prunable = new Prunable(); $models = $prunable->get();
This is useful if you want to integrate cleanup logic directly into your application instead of relying solely on Artisan commands.
License
This project is licensed under the MIT License, meaning you are free to use, modify, and distribute it.