如何在资源中添加图像道具数组?
How to add image props array in resouce?
在laravel 9我想加入$CMSItem对象和相关图片的属性(我用spatie/laravel-medialibrary)
在一项资源中,控制权:
$CMSItem = CMSItem
::getById($cms_item_id)
->with('author')
->first();
$CMSItemImage = [];
foreach ($CMSItem->getMedia(config('app.media_app_name')) as $mediaImage) {
if (File::exists($mediaImage->getPath())) {
$CMSItemImage['url'] = $mediaImage->getUrl();
$imageInstance = Image::load($mediaImage->getUrl());
$CMSItemImage['width'] = $imageInstance->getWidth();
$CMSItemImage['height'] = $imageInstance->getHeight();
$CMSItemImage['size'] = $mediaImage->size;
$CMSItemImage['file_title'] = $mediaImage->file_title;
break;
}
}
在文件 app/Http/Resources/CMSItemResource.php:
class CMSItemResource extends JsonResource
{
public function toArray($request)
{
$dateFunctionality = App::make(DateFunctionality::class);
return [
'id' => $this->id,
'title' => $this->title,
'key' => $this->key,
'text' => $this->text,
'author_id' => $this->author_id,
'author' => new UserResource($this->whenLoaded('author')),
'media_image_url' => $this->media_image_url,
'published' => $this->published,
'published_formatted' => $dateFunctionality->getFormattedDateTime($this->published),
'created_at' => $this->created_at,
'created_at_formatted' => $dateFunctionality->getFormattedDateTime($this->created_at),
'updated_at' => $this->updated_at,
'updated_at_formatted' => $dateFunctionality->getFormattedDateTime($this->updated_at),
];
}
public function with($request)
{
return [
'meta' => [
'version' => getAppVersion()
]
];
}
}
我不知道我可以通过哪种方式在资源中传递图像道具数组?
谢谢!
我找到了在模型中定义属性的决策:
protected $attributes= ['mediaImageProps'];
public function setMediaImagePropsAttribute(array $mediaImageProps)
{
$this->attributes['mediaImageProps'] = $mediaImageProps;
}
public function getMediaImagePropsAttribute(): array
{
return $this->attributes['mediaImageProps'] ?? [];
}
并在控制中分配:
$CMSItem->mediaImageProps= $CMSItemImage;
在laravel 9我想加入$CMSItem对象和相关图片的属性(我用spatie/laravel-medialibrary) 在一项资源中,控制权:
$CMSItem = CMSItem
::getById($cms_item_id)
->with('author')
->first();
$CMSItemImage = [];
foreach ($CMSItem->getMedia(config('app.media_app_name')) as $mediaImage) {
if (File::exists($mediaImage->getPath())) {
$CMSItemImage['url'] = $mediaImage->getUrl();
$imageInstance = Image::load($mediaImage->getUrl());
$CMSItemImage['width'] = $imageInstance->getWidth();
$CMSItemImage['height'] = $imageInstance->getHeight();
$CMSItemImage['size'] = $mediaImage->size;
$CMSItemImage['file_title'] = $mediaImage->file_title;
break;
}
}
在文件 app/Http/Resources/CMSItemResource.php:
class CMSItemResource extends JsonResource
{
public function toArray($request)
{
$dateFunctionality = App::make(DateFunctionality::class);
return [
'id' => $this->id,
'title' => $this->title,
'key' => $this->key,
'text' => $this->text,
'author_id' => $this->author_id,
'author' => new UserResource($this->whenLoaded('author')),
'media_image_url' => $this->media_image_url,
'published' => $this->published,
'published_formatted' => $dateFunctionality->getFormattedDateTime($this->published),
'created_at' => $this->created_at,
'created_at_formatted' => $dateFunctionality->getFormattedDateTime($this->created_at),
'updated_at' => $this->updated_at,
'updated_at_formatted' => $dateFunctionality->getFormattedDateTime($this->updated_at),
];
}
public function with($request)
{
return [
'meta' => [
'version' => getAppVersion()
]
];
}
}
我不知道我可以通过哪种方式在资源中传递图像道具数组?
谢谢!
我找到了在模型中定义属性的决策:
protected $attributes= ['mediaImageProps'];
public function setMediaImagePropsAttribute(array $mediaImageProps)
{
$this->attributes['mediaImageProps'] = $mediaImageProps;
}
public function getMediaImagePropsAttribute(): array
{
return $this->attributes['mediaImageProps'] ?? [];
}
并在控制中分配:
$CMSItem->mediaImageProps= $CMSItemImage;