whilesmart/eloquent-files

File upload and management package for Laravel applications

Installs: 12

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/whilesmart/eloquent-files

dev-main 2026-02-03 09:54 UTC

This package is auto-updated.

Last update: 2026-02-03 10:06:37 UTC


README

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 user
  • POST /api/files - Upload a file
  • GET /api/files/{id} - Get file details
  • DELETE /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