henrotaym / laravel-trustup-media-io
Used by trustup applications to store and retrieve media
Installs: 1 241
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
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-11-27 22:38:26 UTC
README
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> } }