15

Is there any possible way to lazy load a custom attribute on a Laravel model without loading it every time by using the appends property? I am looking for something akin to way that you can lazy load Eloquent relationships.

For instance, given this accessor method on a model:

public function getFooAttribute(){
    return 'bar';
}

I would love to be able to do something like this:

$model = MyModel::all();
$model->loadAttribute('foo');

This question is not the same thing as Add a custom attribute to a Laravel / Eloquent model on load? because that wants to load a custom attribute on every model load - I am looking to lazy load the attribute only when specified.

I suppose I could assign a property to the model instance with the same name as the custom attribute, but this has the performance downside of calling the accessor method twice, might have unintended side effects if that accessor affects class properties, and just feels dirty.

$model = MyModel::all();
$model->foo = $model->foo;

Does anyone have a better way of handling this?

Community
  • 1
  • 1
Andy Noelker
  • 10,949
  • 6
  • 35
  • 47

2 Answers2

29

Is this for serialization? You could use the append() method on the Model instance:

$model = MyModel::all();
$model->append('foo');

The append method can also take an array as a parameter.

Hammerbot
  • 15,696
  • 9
  • 61
  • 103
  • 1
    Hey thanks, this is exactly what I was looking for! I'm glad Laravel has a native way to handle this. I think they only mention `appends` in the docs (and not the singular `append`) which is why I had never heard of this one. – Andy Noelker Oct 11 '16 at 18:21
  • 1
    There is a lot of cool methods I didn't know about. Diving into the Model code is very instructive and I highly recommend it to you to find great tools :) – Hammerbot Oct 11 '16 at 20:16
  • 4
    This doesn't work as described. The `append()` method belongs to the `Model` class, but when you call `MyModel::all()` you're getting back a `Collection` object, which does not contain the `append()` method. You'd have to write: `$model->each(function($instance) { $instance->append('foo'); });` – Soulriser Jan 05 '18 at 21:26
  • 2
    Everytime I find my answer answered on stack, I try to find the answer in documentation too, so I get better skills in going through the documentation. For this matter I found the answer here https://laravel.com/api/8.x/Illuminate/Database/Eloquent/Collection.html#method_append – narrei Jul 27 '21 at 16:29
0

Something like this should work...

public function loadAttribute($name) {
    $method = sprintf('get%sAttribute', ucwords($name));
    $this->attributes[$name] = $this->$method();
}
Rob
  • 12,659
  • 4
  • 39
  • 56