Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

smeranda's avatar

Name Attribute Exhausting PHP Memory

I have a model and corresponding database table that I originally created with a database column titled 'name'. However, for consistency reasons, I changed the title of the column. Then, I created a 'name' attribute on the model:

public function getNameAttribute()
{
    $formatDate = $this->course->start_date->format('D g:i');
    return "<span class='c-enrollment-row__date'>$formatDate</span> <span class='c-enrollment-row__course'>{$this->course->name} Costume Deposit(s)</span>";
}

However, when I use protected $appends = ['name'];, it blows up all my memory and crashes my app. Is there something cached that I need to fix? I ran the following, but no luck:

php artisan cache:clear
php artisan view:clear
php artisan config:clear

TIA

0 likes
12 replies
Dalma's avatar

Is the relationship between this model and your course model a hasOne relationship?

Snapey's avatar

Using with() in one of the models?

smeranda's avatar

@dalma This model (deposit) has:

public function course()
{
    return $this->belongsTo(Course::class);
}

The course model has:

public function deposit()
{
    return $this->hasOne(Deposit::class);
}
Snapey's avatar

always be very careful using with in your models. It is more trouble than its worth in my view since it leads to recursively loading children until you run out of memory

smeranda's avatar

@SNAPEY - I have other withs and they seem to work fine. And the with in the case worked just fine before I changed the name and used the getNameAttribute?

Perhaps I'm not understanding it correctly (very possible!) and I get what you're saying in general about the with, but I'm not sure this is the issues in the case.

Thoughts?

shez1983's avatar

if on the course you have protected with = course, and in deposit you have protected with = deposit then it creates a circular loop where laravel tries to include course in deposit and then deposit in course and so on..

smeranda's avatar

@SHEZ1983 - That make sense, however in this case I do NOT have protected $with = ['course']; on the deposit. The only with I have is on the course model (protected $with = ['deposit'];).

However, on my getNameAttribute, I have $this->course->start_date->format('D g:i'); which is grabbing the course that has the with deposits and is doing the circular loop. Now that makes sense, thank you!

So, aside from removing the with deposits from the course, is there a way to refactor the getNameAttribute?

Snapey's avatar

Well

a) running a query inside an accessor is a bit of a code smell

b) returning html from your model positively stinks.

You might want to install Laravel Debugbar and check how many database queries you are running.

smeranda's avatar

@SNAPEY - I realize HTML in the model isn't ideal, right now it's an easy kludge to push something into a JSON object for testing. My apologies for not mentioning that from the beginning, I was more focused on the issue at hand.

Please or to participate in this conversation.