Append media url trait #1992
-
For a simple API I'm returning a collection without transforming it with Laravel's build in API Resources or Fractal but I just want to include the media urls without too much code. So I came up with this. Just add the trait to the models where the media urls should be appended automatically. <?php
namespace App\Traits;
trait AppendFirstMediaUrls
{
public function attributesToArray()
{
$attributes = parent::attributesToArray();
$this->registerMediaCollections();
foreach ($this->mediaCollections as $mediaCollection) {
$attributes['media_'.$mediaCollection->name] = $this->getFirstMediaUrl($mediaCollection->name);
}
return $attributes;
}
} Is this something to have in this package by default? In my current project I only have |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I'm thinking that it is a bit too opinionated to be in the package. I'd like to keep the package light. I see it as the user responsibility to code up how a media item should be converted to json. |
Beta Was this translation helpful? Give feedback.
I'm thinking that it is a bit too opinionated to be in the package. I'd like to keep the package light. I see it as the user responsibility to code up how a media item should be converted to json.