roui / model-file-manager
A Laravel package to handle file storage on model level
Requires
- php: ^8.3
- intervention/image: ^2.7
README
Introduction
The ModelFileManager package is a Laravel package designed to handle file storage at the model level. It provides features for uploading, deleting, and resizing files. Additionally, it supports multiple file collections and allows you to easily store and manage files such as images, documents, and more.
Requirements
- PHP: 8.3 or higher
- Laravel: 8.x or higher
- Intervention Image: 2.x
Installation
-
Install the package via Composer:
composer require roui/model-file-manager
-
Publish the configuration file:
php artisan vendor:publish --tag=config
-
Ensure the
Intervention Image
package is installed:composer require intervention/image
Configuration
After publishing the configuration, you will find the config file at config/modelfilemanager.php
. You can customize the following settings:
- storage_disk: The default storage disk to use.
- resized_image_path: The path for storing resized images.
- default_collection: The default collection name for file uploads.
Example of config/modelfilemanager.php
:
<?php return [ 'storage_disk' => env('MODEL_FILE_STORAGE_DISK', 'public'), 'resized_image_path' => env('RESIZED_IMAGE_PATH', 'resized/{width}x{height}'), 'default_collection' => env('DEFAULT_FILE_COLLECTION', 'default'), ];
Usage
-
Upload a File: You can upload a file to a model by using the
uploadFile()
method. Specify the file, collection, and disk if needed.$user->uploadFile($file, 'profile_pictures');
-
Delete a File: To delete a file from a specific collection, use the
deleteFile()
method:$user->deleteFile('path/to/file.jpg', 'profile_pictures');
-
Get Files: Retrieve all files from a model’s files column:
$files = $user->getFiles();
-
Get Files from a Collection: Get files from a specific collection, with optional resizing:
$files = $user->getFilesFromCollection('profile_pictures', 300, 300);
-
Replace a File: You can replace an old file with a new one in a collection:
$user->replaceFileInCollection($newFile, 'profile_pictures');
-
Get Resized Image: Get a resized version of a file by specifying dimensions:
$resizedUrl = $user->getResizedImage('path/to/file.jpg', 300, 300);
-
Automatically Cast Files as Array: The files attribute is automatically cast as an array when accessing it:
$files = $user->files; // Returns array of files
Methods Overview
- uploadFile($file, $collection = null, $disk = null): Upload a file to a collection and disk.
- deleteFile($filePath, $collection = null, $disk = null): Delete a file from a collection and disk.
- getFiles(): Get all files from the model.
- getFilesFromCollection($collection = null, $width = null, $height = null, $disk = null): Get files from a collection with optional resizing.
- replaceFileInCollection($newFile, $collection = null, $disk = null): Replace an old file with a new one in a collection.
- getResizedImage($filePath, $width, $height, $disk = null): Get a resized version of an image.
- getFirstFileFromCollection($collection = null, $width = null, $height = null, $disk = null): Get the first file from a collection.
- getFilesAttribute($value): Automatically cast the 'files' column to an array.
License
This package is open-sourced software licensed under the MIT license.