Replies: 1 comment 1 reply
-
Just looking for a bit more clarification 🙂 - is class Post extends Model
{
protected $hidden = ['images'];
public $appends = ['hero_image', 'other_images'];
public function images()
{
return $this->hasMany(Image::class);
}
public function getHeroImageAttribute()
{
if (! $this->relationLoaded('images')) {
return $this->setHidden(array_merge($this->hidden, ['hero_image']));
}
// Some logic to retrieve the hero image
}
public function getOtherImagesAttribute()
{
// Some logic to retrieve other images
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What I'd like to do is to only append some attributes when a specific relation has been loaded. I found a solution that relies on undocumented
getArrayableAppends
, which feels a bit hacky to me. As this is primarily for convenience I'm no so hot on introducing hacky, potentially breaking code.The solution:
Now I can do this in a controller:
Instead of:
Is there any other way to achieve this? Am I way off already in even trying to do this? Thoughts?
Edit: To clarify my intent, I specifically want the array elements added/removed depending on whether a relationship has been loaded so that it matches how relationships are added.
Beta Was this translation helpful? Give feedback.
All reactions