定制 henrotaym/laravel-trustup-media-io 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

henrotaym/laravel-trustup-media-io

Composer 安装命令:

composer require henrotaym/laravel-trustup-media-io

包简介

Used by trustup applications to store and retrieve media

README 文档

README

Compatibility

Laravel Package
8.x / 9.x 1.x
12.x 2.x

Installation

Require package

composer require henrotaym/laravel-trustup-media-io

Set environment variables

TRUSTUP_MEDIA_IO_URL=
TRUSTUP_APP_KEY=

TRUSTUP_SERVER_AUTHORIZATION=

Prepare your models

Here is a example where a post is having a single cover and multiple images.

<?php
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use Henrotaym\LaravelTrustupMediaIo\Models\Traits\HasTrustupMedia;
use Henrotaym\LaravelTrustupMediaIo\Contracts\Models\MediaContract;
use Henrotaym\LaravelTrustupMediaIoCommon\Enums\Media\MediaCollections;
use Henrotaym\LaravelTrustupMediaIo\Contracts\Models\HasTrustupMediaContract;
use Deegitalbe\LaravelTrustupIoExternalModelRelations\Contracts\Models\Relations\ExternalModelRelationContract;

class Post implements HasTrustupMediaContract
{
    use HasTrustupMedia;

    public function getExternalRelationNames(): array
    {
        return [
            'cover',
            'images'
        ];
    }

    /**
     * Cover relation.
     * 
     * @return ExternalModelRelationContract
     */
    public function cover(): ExternalModelRelationContract
    {
        return $this->hasOneTrustupMedia('cover_id');
    }

    /**
     * Images relation
     * 
     * @return ExternalModelRelationContract
     */
    public function images(): ExternalModelRelationContract
    {
        return $this->hasManyTrustupMedia('image_ids');
    }

    /**
     * Getting related images.
     * 
     * @return Collection<int, MediaContract>
     */
    public function getImages(): Collection
    {
        return $this->getExternalModels('images');
    }

    /**
     * Getting related cover.
     * 
     * @return ?MediaContract
     */
    public function getCover(): ?MediaContract
    {
        return $this->getExternalModels('cover');
    }

    /**
     * Setting cover.
     * 
     * @param string|UploadedFile $resource
     * @return static
     */
    public function setCover(string|UploadedFile $resource): self
    {
        // Removing old cover if any.
        $this->removeCover();

        $response = $this->addTrustupMediaFromResource($resource, MediaCollections::IMAGES);

        if (!$response->ok()) return $this;

        $this->cover()->setRelatedModels($response->getFirstMedia());

        return $this;
    }

    /**
     * Removing current cover.
     * 
     * @return static
     */
    public function removeCover(): self
    {
        if (!$this->cover_id) return $this;

        $response = $this->deleteTrustupMediaByUuidCollection(collect($this->cover_id));

        if (!$response->ok()) return $this;

        $this->cover()->setRelatedModels(null);

        return $this;
    }

    /**
     * Adding given image
     * 
     * @param string|UploadedFile $resource
     * @return static
     */
    public function addImage(string|UploadedFile $resource): self
    {
        $response = $this->addTrustupMediaFromResource($resource, MediaCollections::IMAGES);

        if (!$response->ok()) return $this;

        $this->images()->addToRelatedModels($response->getFirstMedia());

        return $this;
    }

    /**
     * Adding given images.
     * 
     * @param Collection<int, string|UploadedFile>
     * @return static
     */
    public function addImages(Collection $resources): self
    {
        $response = $this->addTrustupMediaFromResourceCollection($resources, MediaCollections::IMAGES);

        if (!$response->ok()) return $this;

        $this->images()->addToRelatedModels($response->getMedia());

        return $this;
    }

    /**
     * Removing current images.
     * 
     * @return static
     */
    public function removeImages(): self
    {
        if ($this->image_ids) return $this;

        $response = $this->deleteTrustupMediaByUuidCollection($this->image_ids);

        if (!$response->ok()) return $this;

        $this->images()->setRelatedModels(null);

        return $this;
    }
}

Expose your models (using a resource)

Your resource should look like this.

use Deegitalbe\LaravelTrustupIoExternalModelRelations\Traits\Resources\IsExternalModelRelatedResource;
use Henrotaym\LaravelTrustupMediaIo\Resources\Models\Media;
use Illuminate\Http\Resources\Json\JsonResource;

class PostResource extends JsonResource
{
    use IsExternalModelRelatedResource;

    public function toArray($request)
    {
        return [
            'cover' => new Media($this->whenExternalRelationLoaded('cover')),
            'images' => Media::collection($this->whenExternalRelationLoaded('images'))
        ];
    }
}

Eager loading your relations

Even if you load several relations, only one request will be performed ⚡

use Illuminate\Routing\Controller;
use App\Models\Post;
use App\Http\Resources\PostResource;

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::all()->loadExternalRelations('cover', 'images');

        return PostResource::collection($posts);
    }
}

Getting related models

If your relation is not eager loaded, it will be loaded when using model getter (n+1 requests tho...)

use Illuminate\Routing\Controller;
use App\Models\Post;
use App\Http\Resources\PostResource;

class PostController extends Controller
{
    public function index()
    {
        $post = Post::first();

        $post->getCover() // ?MediaContract
        $post->getImages() // Collection<int, MediaContract>
    }
}

henrotaym/laravel-trustup-media-io 适用场景与选型建议

henrotaym/laravel-trustup-media-io 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.52k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 henrotaym/laravel-trustup-media-io 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 henrotaym/laravel-trustup-media-io 我们能提供哪些服务?
定制开发 / 二次开发

基于 henrotaym/laravel-trustup-media-io 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-31