承接 moh-slimani/media 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

moh-slimani/media

最新稳定版本:0.0.16

Composer 安装命令:

composer require moh-slimani/media

包简介

A Media Model for laravel medialibrary

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package simplifies the integration of Spatie MediaLibrary by offering a streamlined approach to registering and managing media assets within Laravel applications, effortlessly cast media to a simple format, leveraging a path generator that enhances readability and organization. Seamlessly handle media registration while maintaining all functionalities of The MediaLibrary package.

Installation

You can install the package via composer:

composer require moh-slimani/media

You must publish and run the migrations,this will add soft delete to the media model

php artisan vendor:publish --tag="media-migrations"
php artisan migrate

change the media_model in the media library config file

config/media-library.php

    ...
    /*
     * The fully qualified class name of the media model.
     */
    'media_model' => MohSlimani\Media\Models\Media::class,
    
    ...    

Optional : you can change the path generator in the media library config file

    ...
    
    /*
     * The class that contains the strategy for determining a media file's path.
     */
    'path_generator' => MohSlimani\Media\Helpers\MediaPathGenerator::class,

    ...

Usage

use MohSlimani\Media\Traits\UseMediaModel
use MohSlimani\Media\Media
use Spatie\MediaLibrary\HasMedia;


class User extends Authenticatable implements HasMedia
{

    // you don't need to user InteractsWithMedia
    use HasApiTokens, HasFactory, Notifiable, UseMediaModel;


    /**
     * This array should contain the list of media keys to be registered.
     *
     * @var array $files
     * @example ['photo' => Media::SINGLE_FILE, 'files' => Media::MULTIPLE_FILES]
     */
    protected array $files = [
        'photo' => Media::SINGLE_FILE, 
        'cv', // Media::SINGLE_FILE is the default
        'files' => Media::MULTIPLE_FILES
    ];

    ...

After that you can add files it like you used to using the medialibrary package

/** @var File $photo */
$user->addMedia($photo)->toMediaCollection('photo');

// Or use the included function
$user->addMediaFiles($photo, 'photo');

the function addMediaFiles to simplify the process of adding media files to collections. This function allows you to add a single file to a specified collection and provides an option to keep or delete existing files in the collection. it will also generate a unique code based on current time to prepend to the file name You can find more information about this and other changes in the CHANGELOG.

you can get the files like this

    $user->photo
 
    [
        "id" => 15,
        "name" => "IMG_7833",
        "url" => "http://media.test/storage/Users/1/photo/IMG_7833.jpg",
        "size" => 249686,
        "mime" => "image/jpeg",
        "type" => "image",
        "created_at" => Illuminate\Support\Carbon @1705898712 {#6527
          date: 2024-02-19 00:00:00.0 UTC (+00:00),
        },
        "updated_at" => Illuminate\Support\Carbon @1705898712 {#6782
          date: 2024-02-19 00:00:00.0 UTC (+00:00),
        },
    ]

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 42
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-19