whilesmart/eloquent-files
File upload and management package for Laravel applications
81
| Install | |
|---|---|
composer require whilesmart/eloquent-files |
|
| PHP: | ^8.2 |
| License: | MIT |
| Last Updated: | Apr 25, 2026 |
| Links: | GitHub · Packagist |
Maintainer: whilesmart
Eloquent Files
File upload and management package for Laravel applications.
Installation
composer require whilesmart/eloquent-files
Configuration
Publish the configuration file:
php artisan vendor:publish --tag=files-config
Configuration Options
return [
'disk' => 'public', // Storage disk
'upload_path' => 'uploads', // Base path for uploads
'max_size' => 5120, // Max file size in KB (5MB)
'allowed_mimes' => 'image', // Allowed mime types
'register_routes' => true, // Auto-register routes
'route_prefix' => 'api', // Route prefix
'route_middleware' => ['api', 'auth:sanctum'],
];
Usage
Adding Files to a Model
Add the HasFiles trait to any model that should own files:
use Whilesmart\Files\Traits\HasFiles;
class User extends Authenticatable
{
use HasFiles;
}
API Endpoints
When routes are registered, the following endpoints are available:
GET /api/files- List files for authenticated userPOST /api/files- Upload a fileGET /api/files/{id}- Get file detailsDELETE /api/files/{id}- Delete a file
Uploading Files
curl -X POST /api/files \
-H "Authorization: Bearer {token}" \
-F "file=@image.jpg"
Response Format
{
"success": true,
"data": {
"id": 1,
"filename": "upload_abc123_1234567890.jpg",
"original_name": "image.jpg",
"url": "http://example.com/storage/uploads/1/upload_abc123.jpg",
"mime_type": "image/jpeg",
"size": 102400,
"metadata": {
"width": 1920,
"height": 1080
},
"created_at": "2024-01-01T00:00:00Z"
}
}
Using the File Model Directly
use Whilesmart\Files\Models\File;
// Get user's files
$files = File::ownedBy($user)->get();
// Delete a file (removes from storage and database)
$file->deleteFile();
// Check ownership
if ($file->isOwnedBy($user)) {
// ...
}
Testing
composer test
License
MIT