henrotaym / laravel-trustup-media-io
被 trustup 应用用于存储和检索媒体
v1.3.3
2024-05-27 21:24 UTC
Requires
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.5
- dev-main
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.2-alpha.31
- v1.0.2-alpha.30
- v1.0.2-alpha.29
- v1.0.2-alpha.28
- v1.0.2-alpha.27
- v1.0.2-alpha.26
- v1.0.2-alpha.25
- v1.0.2-alpha.24
- v1.0.2-alpha.23
- v1.0.2-alpha.22
- v1.0.2-alpha.21
- v1.0.2-alpha.20
- v1.0.2-alpha.19
- v1.0.2-alpha.18
- v1.0.2-alpha.17
- v1.0.2-alpha.16
- v1.0.2-alpha.15
- v1.0.2-alpha.14
- v1.0.2-alpha.13
- v1.0.2-alpha.12
- v1.0.2-alpha.11
- v1.0.2-alpha.10
- v1.0.2-alpha.9
- v1.0.2-alpha.8
- v1.0.2-alpha.7
- v1.0.2-alpha.6
- v1.0.2-alpha.5
- v1.0.2-alpha.4
- v1.0.2-alpha.3
- v1.0.2-alpha.2
- v1.0.2-alpha.1
- v1.0.2-alpha.0
- v1.0.1
- v1.0.1-alpha.2
- v1.0.1-alpha.1
- v1.0.1-alpha.0
- v1.0.0
This package is auto-updated.
Last update: 2024-09-27 22:06:35 UTC
README
安装
需要安装的包
composer require henrotaym/laravel-trustup-media-io
设置环境变量
TRUSTUP_MEDIA_IO_URL= TRUSTUP_APP_KEY= TRUSTUP_SERVER_AUTHORIZATION=
准备您的模型
以下是一个示例,其中一篇文章有一个封面和多个图片。
<?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; } }
公开您的模型(使用资源)
您的资源应该看起来像这样。
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')) ]; } }
预加载您的关联
即使您加载了多个关联,也只会执行一个请求 ⚡
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); } }
获取相关模型
如果您的关联没有预加载,当使用模型获取器时它将被加载(但是会有 n+1 个请求...)
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> } }