medialibrary-development

Build and use features of spatie/laravel-medialibrary, including associating files with Eloquent models, defining media collections and conversions, generating responsive images, and obtaining media URLs and paths.

Last repository push
GitHub Stars
147
Forks
61
Issues / PRs
5

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "medialibrary-development" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-1e8995250e9b9413cb46eedb78fa56e357a6fb89cefbf68de469541288ccbfa7. Verify the upstream source and version against https://github.com/gdarko/laravel-vue-starter before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

github.com

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

Build and use features of spatie/laravel-medialibrary, including associating files with Eloquent models, defining media collections and conversions, generating responsive images, and obtaining media URLs and paths.

0 Downloads

Introduction source: third_party · gdarko · 0.0.1 Reference

SkillPi interpretation

Tasks
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.
Software, accounts and paid services
Every model that should have media must implement `HasMedia` and use the `InteractsWithMedia` trait:
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: medialibrary-development
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.
license: MIT
metadata:
  author: Spatie

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

Current listing version

0.0.1

Verify the source revision ↗

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value85/100

Highlights

  • 提供 spatie/laravel-medialibrary 的完整开发指南,涵盖模型关联、媒体集合、转换、响应式图片等核心功能
  • 面向 Laravel 开发者解决文件管理的实际痛点,实用性强
Trigger Precision90/100

Highlights

  • 触发条件清晰明确,覆盖文件上传、媒体附件、image processing 等典型场景
  • 边界清晰,不易与其他 Skill 重叠
Structure and Standards95/100

Areas to improve

  • description写错位置 Reported severity: Minor

    顶层 frontmatter 中 description 字段为空,实际描述内容位于 skill_body 内部

    Recommendation: 建议将 description 内容移至顶层 frontmatter 的 description 字段

  • 缺少端到端完整示例 Reported severity: Minor

    提供了多个代码片段,但缺少一个完整的端到端使用示例

    Recommendation: 建议添加一个完整的示例,展示从模型定义到媒体上传、转换、获取 URL 的完整流程

  • 文档过长 Reported severity: Minor

    文档内容约450行,略长

    Recommendation: 建议适当精简内容,或将部分内容移至引用文件中

Highlights

  • 结构完整,包含多个实用章节,步骤清晰
  • 提供了详细的 Do and Don't 最佳实践指南
Execution Reliability90/100

Highlights

  • 代码示例完整,参数使用正确
  • 覆盖了常见异常和错误用法,并给出了规避建议
Evidence Verifiability (mean of the first four)90/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

These common operations were not identified in the main document. This does not mean no permissions or risks exist.

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

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

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.