A Laravel package to handle file storage on model level
roui/model-file-manager is a Laravel package for a laravel package to handle file storage on model level.
It currently has 4 GitHub stars and 2 downloads on Packagist (latest version v1.0.0).
Install it with composer require roui/model-file-manager.
Discover more Laravel packages by roui
or browse all Laravel packages to compare alternatives.
Last updated
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.
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
After publishing the configuration, you will find the config file at config/modelfilemanager.php. You can customize the following settings:
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'),
];
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
This package is open-sourced software licensed under the MIT license.