Tech Verse Logo
Enable dark mode
Laravel Artifact: Manage Your Media Easily

Laravel Artifact: Manage Your Media Easily

Md. Mostafijur RahmanMMd. Mostafijur Rahman

Md. Mostafijur Rahman

2 min read

Laravel Artifact is a lightweight package for media management in Laravel applications. It makes file uploads, storage, and retrieval easy — with support for public/private disks, signed URLs, and automatic deduplication.

Features

  • Simple file upload and storage management

  • Support for multiple storage disks (local, S3, etc.)

  • Automatic deduplication to prevent duplicate files

  • Signed URLs for secure file access

  • Clean one-to-one and one-to-many file relationships

  • Automatic metadata tracking (filename, MIME type, size)

Installation

Install via Composer:

composer require laraveljutsu/laravel-artifact

Publish the configuration and run migrations:

php artisan vendor:publish --tag="laravel-artifact"

Usage

Configure the package

Configure the package in the config/artifact.php file.

return [
    'model' => LaravelJutsu\Artifact\Artifact::class,
    'table_name' => 'artifacts',
    'routes' => [
        'prefix' => env('ARTIFACT_ROUTE_PREFIX', 'artifacts'),
        'middleware' => ['web'],
    ],
    'signed_url' => [
        'expiration_minutes' => env('ARTIFACT_SIGNED_URL_EXPIRATION', 60),
    ],
];

// .env
ARTIFACT_ROUTE_PREFIX=artifacts
ARTIFACT_SIGNED_URL_EXPIRATION=60

After configuring the package, run the migrations:

php artisan optimize
php artisan migrate

Add the trait to your model

<?php

use LaravelJutsu\Artifact\Concerns\HasArtifacts;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasArtifacts;

    // Single file relationship
    public function avatar()
    {
        return $this->singleArtifact('avatar');
    }

    // Multiple files relationship
    public function documents()
    {
        return $this->manyArtifacts('documents');
    }
}

Store files

$user = User::find(1);
$file = request()->file('avatar');

// Single file (one-to-one)
$artifact = $user->avatar()->store($file);

// Multiple files (one-to-many)
$artifacts = $user->documents()->store(request()->file('documents'));

// Specify storage disk
$artifact = $user->avatar()->store($file); // Uses default disk
$artifacts = $user->documents()->store($files, 'public');

Access files and metadata

$avatar = $user->avatar;

if ($avatar) {
    // Get URLs
    echo $avatar->rawUrl();            // Direct URL (public disks only)
    echo $avatar->streamUrl();         // Streaming URL (works for all disks)
    echo $avatar->signedUrl();         // Permanent signed URL
    echo $avatar->temporarySignedUrl(60); // Expiring signed URL (60 minutes)

    // Access metadata
    echo $avatar->file_name;   // Original filename
    echo $avatar->mime_type;   // File MIME type
    echo $avatar->size;        // File size in bytes
    echo $avatar->disk;        // Storage disk name
}

// Working with multiple files
foreach ($user->documents as $document) {
    echo $document->file_name . ' (' . $document->size . ' bytes)';
    echo $document->streamUrl();
}

Requirements

  • PHP 8.2+

  • Laravel 10+

Md. Mostafijur RahmanMMd. Mostafijur Rahman

WRITTEN BY

Md. Mostafijur Rahman

    Latest Posts

    View All

    React Context: When to Reach for a Store

    React Context: When to Reach for a Store

    Custom React Hooks: Abstraction vs Indirection

    Custom React Hooks: Abstraction vs Indirection

    Stop Wasting React Performance on memo and useMemo

    Stop Wasting React Performance on memo and useMemo

    Next.js Partial Prerendering: Static Shells, Dynamic Holes

    Next.js Partial Prerendering: Static Shells, Dynamic Holes

    Stop Re-rendering: When to Drop useEffect in React 19

    Stop Re-rendering: When to Drop useEffect in React 19

    Fixing Next.js Hydration Mismatch Errors in React 19

    Fixing Next.js Hydration Mismatch Errors in React 19

    Generating Open Graph Images at the Edge in Next.js 16

    Generating Open Graph Images at the Edge in Next.js 16

    Fix Next.js Third Party Script Performance

    Fix Next.js Third Party Script Performance

    Next.js 16 Testing Strategy: Unit, Component, E2E

    Next.js 16 Testing Strategy: Unit, Component, E2E

    Shrinking Next.js Bundle Size: Analyzer & Barrel Fixes

    Shrinking Next.js Bundle Size: Analyzer & Barrel Fixes