deegitalbe/laravel-trustup-io-external-model-relations
Composer 安装命令:
composer require deegitalbe/laravel-trustup-io-external-model-relations
包简介
Basic versioned php package.
README 文档
README
Compatibility
| Laravel | Package |
|---|---|
| 8.x / 9.x | 1.x |
| 12.x | 2.x |
Installation
Require package
composer require deegitalbe/laravel-trustup-io-external-model-relations
Define a loader
Each external relation should have its own loader that is implementing ExternalModelRelationLoadingCallbackContract.
<?php namespace App\Models\Relations; use Deegitalbe\LaravelTrustupIoAuthClient\Contracts\Api\Endpoints\Auth\UserEndpointContract; use Deegitalbe\LaravelTrustupIoExternalModelRelations\Contracts\Models\Relations\ExternalModelRelationLoadingCallbackContract; use Illuminate\Support\Collection; class TrustupUserRelationLoadingCallback implements ExternalModelRelationLoadingCallbackContract { /** * User endpoint. * * @var UserEndpointContract */ protected UserEndpointContract $endpoint; /** * Constructing instance. * * @param UserEndpointContract $endpoint * @return void */ public function __construct(UserEndpointContract $endpoint) { $this->endpoint = $endpoint; } public function load(Collection $identifiers): Collection { return $this->endpoint->byIds($identifiers); } }
Preparing your models
Your model having external relationships should look like this
<?php namespace App\Models; use Illuminate\Support\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Casts\AsCollection; use App\Models\Relations\TrustupUserRelationLoadingCallback; use Deegitalbe\LaravelTrustupIoExternalModelRelations\Contracts\Models\ExternalModelContract; use Deegitalbe\LaravelTrustupIoExternalModelRelations\Traits\Models\IsExternalModelRelatedModel; use Deegitalbe\LaravelTrustupIoExternalModelRelations\Contracts\Models\ExternalModelRelatedModelContract; use Deegitalbe\LaravelTrustupIoExternalModelRelations\Contracts\Models\Relations\ExternalModelRelationContract; class Post extends Model implements ExternalModelRelatedModelContract { use IsExternalModelRelatedModel; /** * Getting external relation names. * * @return array<int, string> */ public function getExternalRelationNames(): array { return [ 'contributors', 'creator' ]; } /** * Defining contributors relation. * * @return ExternalModelRelationContract */ public function contributors(): ExternalModelRelationContract { return $this->hasManyExternalModels(app()->make(TrustupUserRelationLoadingCallback::class), 'contributor_ids'); } /** * Defining creator relation. * * @return ExternalModelRelationContract */ public function creator(): ExternalModelRelationContract { return $this->belongsToExternalModel(app()->make(TrustupUserRelationLoadingCallback::class), 'creator_id'); } /** * Getting related contributors. * * @return Collection<int, ExternalModelContract> */ public function getContributors(): Collection { return $this->getExternalModels('contributors'); } /** * Getting related creator. * * @return ?ExternalModelContract */ public function getCreator(): ?ExternalModelContract { return $this->getExternalModels('creator'); } /** * Setting related creator (OPTIONAL). * * @param ?int $creatorId * @return static */ public function setCreator(?int $creatorId): self { $this->creator()->setRelatedModelsByIds($creatorId); return $this; } /** * Setting related contributors (OPTIONAL). * * @param Collection<int, int> $contributorIds * @return static */ public function setContributors(Collection $contributorIds): self { $this->contributors()->setRelatedModelsByIds($contributorIds); return $this; } }
Exposing your models by creating a resource
If you wanna expose your model, here is an example resource based on previous section model
<?php namespace App\Http\Resources; use Deegitalbe\LaravelTrustupIoExternalModelRelations\Resources\TrustupUserResource; use App\Resources\TrustupUserResource; use Deegitalbe\LaravelTrustupIoExternalModelRelations\Resources\ExternalModelRelatedResource; class PostResource extends ExternalModelRelatedResource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable */ public function toArray($request) { return [ 'id' => $this->id, 'title' => $this->title, 'text' => $this->text, 'created_at' => $this->created_at, 'contributors' => TrustupUserResource::collection($this->whenExternalModelsLoaded('contributors')), 'creator' => new TrustupUserResource($this->whenExternalModelsLoaded('creator')) ]; } }
Eager load collections
Only one request will be performed even if you load multiple relations ⚡⚡⚡⚡
use Illuminate\Routing\Controller; class PostController extends Controller { public function index() { return PostResource::collection(Post::all()->loadExternalRelations('contributors', 'creator')); } }
deegitalbe/laravel-trustup-io-external-model-relations 适用场景与选型建议
deegitalbe/laravel-trustup-io-external-model-relations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.22k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 deegitalbe/laravel-trustup-io-external-model-relations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 deegitalbe/laravel-trustup-io-external-model-relations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-11