sextanet / laravel-files
Connect real files with your models in Laravel 11+
Fund package maintenance!
SextaNet
Requires
- php: ^8.3
- illuminate/contracts: ^11.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
This package is auto-updated.
Last update: 2025-09-29 01:25:04 UTC
README
Upload seamless Storage and Database files in Laravel
It supports:
- Native Laravel filesystems, such as
public
,local
,s3
disks - Local Temporary URLs
- Scoped disks
Requirements
- Laravel 11+
- PHP 8.3+
Installation
composer require sextanet/laravel-files
Publish and run the migrations with:
php artisan vendor:publish --tag="files-migrations"
php artisan migrate
Usage
use SextaNet\LaravelFiles\HasFiles; // 👈 1. Import class YourModel extends Model { use HasFiles; // 👈 2. Use it }
We're ready! You can reuse it in each model, any times!
Add (and Store)
$uploaded_file = request()->your_file; $file = $user->addFile($uploaded_file); // using your default disk from config/filesystems.php
Or passing the name, for example: new_name
, will preserve the extension, so, it will store as new_name.mp4
$uploaded_file = request()->your_file; // let's supose you have "a_video.mp4" $file = $user->addFile($uploaded_file, 'new_name'); // will generate new_name.mp4
Get
You can get the path
, url
and temporary_url
for each file
$path = $file->path(); $url = $file->url(); $temporary_url = $file->temporaryUrl();
Get owner
return $file->owner;
Get all files
return $user->files;
Get the latest file
return $user->latestFile;
Download
Without parameters
return $file->download();
With custom parameters
$name = 'custom_name'; $headers = []; return $file->download($name, $headers);
Turn off the extensions
By default it preserves the extension. You can disable that by passing preserveExtension
to false
return $file->download(preserveExtension: false);
Advanced usage
Passing a type
Type doesn't represent a real type or mimetype, it's only an optional field
$uploaded_file = request()->your_file; // Store $user->addFile($uploaded_file, type: 'document'); // Get $user->files()->type('document')->get();
Passing custom minutes in each implementation
$temporary_url = $file->getTemporaryUrl(20); // 20 minutes
Global usage
Passing custom minutes globally
// Don't forget to import the LaravelFiles facade use SextaNet\LaravelFiles\Facades\LaravelFiles; // Set temporary URL for 120 minutes LaravelFiles::setTemporaryUrlMinutes(120); $temporary_url = $file->getTemporaryUrl(); // Will return 120 minutes
Passing a different disk in a specific instance
// Don't forget to import the LaravelFiles facade use SextaNet\LaravelFiles\Facades\LaravelFiles; // Store on another disk, like s3 LaravelFiles::setDisk('s3');
Custom keys
By default, we use the same CURRENT_DISK
that you have in your .env
file. If you want to force to use different values, you can add these keys with different values:
Another disk
FILES_DISK=s3
Custom minutes
FILES_TEMPORARY_URL_MINUTES=5
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.