Overview

How to attach, upload, and organize files

Usage

The package centers on the HasAttachments trait. Add it to any model and you gain a full attachment API; pair it with the upload actions to store files, and the Folder model to organize them.

The HasAttachments trait

php
use Illuminate\Database\Eloquent\Model;
use OiLab\OiLaravelAttachments\Concerns\HasAttachments;

class Product extends Model
{
    use HasAttachments;
}

This trait adds:

MethodPurpose
attachFile($file, $collection, $sort)Attach a single file
detachFile($file, $collection)Remove a file from the model
syncAttachments($files, $collection)Replace a whole collection
syncAttachmentsIfChanged($files, $collection)Replace only when ids/order differ
reorderAttachments($order, $collection)Update sort positions
attachments($collection)MorphMany relation, ordered by sort
singleAttachment($collection)MorphOne relation for single-file collections
attached_filesAccessor returning a Collection<File>

In this section

  • Attachments — attach, detach, sync, and reorder files
  • Uploads — store uploaded files with the action classes
  • Files & Folders — work with the File and Folder models
Project under MIT License.
Design by