sextanet/laravel-files

Connect real files with your models in Laravel 11+

1.1.1 2025-09-29 01:23 UTC

This package is auto-updated.

Last update: 2025-09-29 01:25:04 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Upload seamless Storage and Database files in Laravel

It supports:

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.