Agent Skillsrelaticle/relaticle › medialibrary-development

medialibrary-development

GitHub

指导在 Laravel 中使用 spatie/laravel-medialibrary 库处理媒体资源,包括模型关联、文件上传、媒体集合定义、图片转换及响应式图像处理等开发任务。

.claude/skills/medialibrary-development/SKILL.md relaticle/relaticle

Trigger Scenarios

需要在 Laravel Eloquent 模型中关联和管理文件 实现图片上传、视频处理或生成响应式图片 定义媒体集合或配置图片转换规则

Install

npx skills add relaticle/relaticle --skill medialibrary-development -g -y
More Options

Non-standard path

npx skills add https://github.com/relaticle/relaticle/tree/main/.claude/skills/medialibrary-development -g -y

Use without installing

npx skills use relaticle/relaticle@medialibrary-development

指定 Agent (Claude Code)

npx skills add relaticle/relaticle --skill medialibrary-development -a claude-code -g -y

安装 repo 全部 skill

npx skills add relaticle/relaticle --all -g -y

预览 repo 内 skill

npx skills add relaticle/relaticle --list

SKILL.md

Frontmatter
{
    "name": "medialibrary-development",
    "license": "MIT",
    "metadata": {
        "author": "Spatie"
    },
    "description": "Build and work with spatie\/laravel-medialibrary features including associating files with Eloquent models, defining media collections and conversions, generating responsive images, and retrieving media URLs and paths."
}

Media Library Development

Overview

Use spatie/laravel-medialibrary to associate files with Eloquent models. Supports image/video conversions, responsive images, multiple collections, and various storage disks.

When to Activate

  • Activate when working with file uploads, media attachments, or image processing in Laravel.
  • Activate when code references HasMedia, InteractsWithMedia, the Media model, or media collections/conversions.
  • Activate when the user wants to add, retrieve, convert, or manage files attached to Eloquent models.

Scope

  • In scope: media uploads, collections, conversions, responsive images, custom properties, file retrieval, path/URL generation.
  • Out of scope: general file storage without Eloquent association, non-Laravel frameworks.

Workflow

  1. Identify the task (model setup, adding media, defining conversions, retrieving files, etc.).
  2. Read references/medialibrary-guide.md and focus on the relevant section.
  3. Apply the patterns from the reference, keeping code minimal and Laravel-native.

Core Concepts

Model Setup

Every model that should have media must implement HasMedia and use the InteractsWithMedia trait:

use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class BlogPost extends Model implements HasMedia
{
    use InteractsWithMedia;
}

Adding Media

$blogPost->addMedia($file)->toMediaCollection('images');
$blogPost->addMediaFromUrl($url)->toMediaCollection('images');
$blogPost->addMediaFromRequest('file')->toMediaCollection('images');

Defining Collections

public function registerMediaCollections(): void
{
    $this->addMediaCollection('avatar')->singleFile();
    $this->addMediaCollection('downloads')->useDisk('s3');
}

Defining Conversions

use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Image\Enums\Fit;

public function registerMediaConversions(?Media $media = null): void
{
    $this->addMediaConversion('thumb')
        ->fit(Fit::Contain, 300, 300)
        ->nonQueued();
}

Retrieving Media

$url = $model->getFirstMediaUrl('images');
$thumbUrl = $model->getFirstMediaUrl('images', 'thumb');
$allMedia = $model->getMedia('images');

Do and Don't

Do:

  • Always implement the HasMedia interface alongside the InteractsWithMedia trait.
  • Use ?Media $media = null as the parameter for registerMediaConversions().
  • Call ->toMediaCollection() to finalize adding media.
  • Use ->nonQueued() for conversions that should run synchronously.
  • Use ->singleFile() on collections that should only hold one file.
  • Use Spatie\Image\Enums\Fit enum values for fit methods.

Don't:

  • Don't forget to run php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations" before migrating.
  • Don't use env() for disk configuration; use config() or set it in config/media-library.php.
  • Don't call addMedia() without calling toMediaCollection() — the media won't be saved.
  • Don't reference conversion names that aren't registered in registerMediaConversions().

References

  • references/medialibrary-guide.md

Version History

  • 1f433f8 Current 2026-08-20 14:43

Same Skill Collection

.claude/skills/echo-development/SKILL.md
.claude/skills/laravel-query-builder/SKILL.md
.claude/skills/socialite-development/SKILL.md
.claude/skills/spatie-javascript/SKILL.md
.claude/skills/spatie-laravel-php/SKILL.md
.claude/skills/spatie-security/SKILL.md
.claude/skills/spatie-version-control/SKILL.md
.github/skills/custom-fields-development/SKILL.md
.github/skills/flowforge-development/SKILL.md
.claude/skills/agent-browser-relaticle/SKILL.md
.claude/skills/ai-sdk-development/SKILL.md
.claude/skills/business-review/SKILL.md
.claude/skills/cashier-stripe-development/SKILL.md
.claude/skills/configuring-horizon/SKILL.md
.claude/skills/fortify-development/SKILL.md
.claude/skills/infer-conventions/SKILL.md
.claude/skills/laravel-best-practices/SKILL.md
.claude/skills/livewire-development/SKILL.md
.claude/skills/mcp-development/SKILL.md
.claude/skills/passport-development/SKILL.md
.claude/skills/pennant-development/SKILL.md
.claude/skills/pest-testing/SKILL.md
.claude/skills/screenshot-with-callout/SKILL.md
.claude/skills/tailwindcss-development/SKILL.md
.github/skills/livewire-development/SKILL.md
.github/skills/manual-testing/SKILL.md
.github/skills/tailwindcss-development/SKILL.md

Metadata

Files
0
Version
1f433f8
Hash
dc39f58d
Indexed
2026-08-20 14:43

- 위키
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-25 11:40
浙ICP备14020137号-1 $방문자$