PHPackages                             codeitamarjr/laravel-attachments - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. codeitamarjr/laravel-attachments

ActiveLibrary[File &amp; Storage](/categories/file-storage)

codeitamarjr/laravel-attachments
================================

Persist and manage polymorphic file attachments in Laravel applications.

v0.6.0(3mo ago)216MITPHPPHP ^8.3CI passing

Since Oct 28Pushed 2mo agoCompare

[ Source](https://github.com/codeitamarjr/laravel-attachments)[ Packagist](https://packagist.org/packages/codeitamarjr/laravel-attachments)[ Docs](https://github.com/codeitamarjr/Laravel-Attachments)[ RSS](/packages/codeitamarjr-laravel-attachments/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (9)Versions (7)Used By (0)

Laravel Attachments
===================

[](#laravel-attachments)

[![Latest Version on Packagist](https://camo.githubusercontent.com/79278040057fb4b5c1247aff192d0c506c1a988f5fb0218f5830806484e31f02/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f64656974616d61726a722f6c61726176656c2d6174746163686d656e74732e737667)](https://packagist.org/packages/codeitamarjr/laravel-attachments)[![Tests](https://github.com/codeitamarjr/Laravel-Attachments/actions/workflows/tests.yml/badge.svg)](https://github.com/codeitamarjr/Laravel-Attachments/actions/workflows/tests.yml)[![License](https://camo.githubusercontent.com/c6ec7f75b9c9921e9dcfae572e1a9784a98dc34a21d79c5b006473a80699f7fe/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f636f64656974616d61726a722f4c61726176656c2d4174746163686d656e7473)](LICENSE)

`codeitamarjr/laravel-attachments` adds a small attachment layer/model on top of Laravel filesystem.

It gives you:

- A polymorphic `attachments` table for any Model
- A `HasAttachments` trait with explicit single-file and multi-file collection helpers
- An `AttachmentService` for storing, replacing, and deleting files
- Public/private visibility handling with URL abstraction

Why This Package Exists
-----------------------

[](#why-this-package-exists)

In many Laravel applications, user file uploads end up being handled:

- store the file in one place
- save metadata somewhere else
- manually wire the metadata back to a model
- remember to clean up storage when the model or file is replaced or deleted

This package creates the `attachments` table and the `Attachment` model, and it gives you a reusable way to attach files to any model and persist their metadata on the Attachment model, updating and deleting files being handled by the Trait and Service.

Quick Start
-----------

[](#quick-start)

```
composer require codeitamarjr/laravel-attachments
php artisan vendor:publish --tag=attachments-migrations
php artisan migrate
```

Sample usage:

```
class Invoice extends Model implements Attachable
{
    use HasAttachments;
}

// Store a new file in the "document" collection for the invoice model, associating the uploader by their authenticated ID:
$attachments->store($invoice, $file, 'document', auth()->id());
```

Contents
--------

[](#contents)

- [Laravel Attachments](#laravel-attachments)
    - [Why This Package Exists](#why-this-package-exists)
    - [Quick Start](#quick-start)
    - [Contents](#contents)
    - [Requirements](#requirements)
    - [Configuration](#configuration)
    - [Basic Usage](#basic-usage)
    - [Storing Files](#storing-files)
    - [Replacing Files](#replacing-files)
    - [Deleting Files](#deleting-files)
    - [Collection Semantics](#collection-semantics)
    - [Attachment Model](#attachment-model)
    - [Testing](#testing)
    - [Changelog](#changelog)
    - [License](#license)

Requirements
------------

[](#requirements)

- PHP 8.3+
- Laravel 11, 12, or 13

Configuration
-------------

[](#configuration)

The published `config/attachments.php` file exposes:

- `disk`: the filesystem disk used to store uploaded files
- `visibility`: the default visibility for stored attachments
- `uploader_model`: model used by the `uploader()` relationship
- `uploader_foreign_key`: attachments column used for the uploader relationship
- `directory`: the base directory inside that disk
- `private_url_ttl`: how long private temporary URLs should remain valid

By default the package reads:

```
ATTACHMENTS_DISK=public // Optional, but defaults to public if not set. Make sure the selected disk is properly configured in config/filesystems.php and exposed in your application when applicable, Private attachments require a filesystem driver that supports Laravel temporary URLs.
ATTACHMENTS_VISIBILITY=public // Optional, but defaults to public if not set. Can be overridden per attachment when storing.
ATTACHMENTS_UPLOADER_MODEL="App\\Models\\User" // Optional, but defaults to User if not set
ATTACHMENTS_UPLOADER_FOREIGN_KEY=uploaded_by // Nullable by default, but required if you set an uploader model
ATTACHMENTS_DIRECTORY=attachments // Base directory for all attachments in the selected disk
ATTACHMENTS_PRIVATE_URL_TTL=5 // Minutes for the temporary URL to remain valid
```

Basic Usage
-----------

[](#basic-usage)

Add the `HasAttachments` trait to any model that should own files:

```
