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

jeroenvanrensen's avatar

Undefined property, but other columns do work

Hi everyone,

I'm creating a website with Laravel, and I've got the following problem:

I get this error:

 Undefined property: App\Moon\MoonCrud::$primary_column

When I use this code:

public function getPrimaryColumnAttribute()
{
    return $this->primary_column;
}

But If I use this code:

return $this->name_plural;

I got Pages, exactly what's in the database.

I dubble dubble dubble checked if the column primary_column exists, and it does.

Can anyone please help me?

Thank you! Jeroen

If you need more information, please ask me.

0 likes
6 replies
Nakov's avatar

Why would you name the accessor method the same as your column, unless you want to modify the existing value?

In case you want to modify an existing value, then take a look at the documentation for its usage:

https://laravel.com/docs/master/eloquent-mutators#accessors-and-mutators

Basically you need this:

public function getPrimaryColumnAttribute($value)
{
    return $value;
}

Because calling $this->primary_column is like calling the same method over and over again.

jeroenvanrensen's avatar

Hi @nakov.

Sorry, I don't understand you.

I want to get the primary_column value out of the moon_cruds table.

If I try it with the name_plural value it does work but with primary_column it doen not.

I hope you can help me.

Jeroen

Nakov's avatar

@jeroenvanrensen

What I am saying is that this:

public function getPrimaryColumnAttribute()
{
    return $this->primary_column;
}

doesn't make sense.. you can take the value out of the row and column by finding the row which you need and then directly access the column if it exists as you said:

$moonCrud = MoonCrud::find(1);

$moonCrud->primary_column;

Should give you what you want.

jeroenvanrensen's avatar

Yes I know but my system is more complex.

I have got a table, like pagesand a table called moon_cruds. In the last table I've got a row where is called to the first table.

I want to get the primary_column out of the moon_cruds table (like title or name), and get that value from the pages table.

I hope you now understand.

Jeroen

gakharyasir's avatar

Try the following solution

public function getPrimaryColumnAttribute($primaryColumn)
{
    return $primaryColumn;
}

Your code is not working because return $this->primary_column; this statement in your accessor is causing an infinite loop as the accessor is called every time when primary_column is being accessed. In your case when you access the primary_column the accessor is called and in accessor, you are again accessing the primary_column which again calls the accessor itself and the loop continues so on.

The solution is to pass a value as a parameter in the accessor. Eloquent will automatically map this parameter to primary_column value, now you can do whatever you want to do with the value.

christmex's avatar

Hi @jeroenvanrensen I think @nakov already explained it clearly, I got the same error as you, I was struggling with that for a few hours get confused with that, but when I come here I got the point that when u wanted to modify the value of your column

you can easily do that with this

public function getPrimaryColumnAttribute($value){ 
     return $value;
    //or
     return $this->attributes['primary_column']; //no need to send $value as param
}

or something like this

use Illuminate\Database\Eloquent\Casts\Attribute;
protected function primaryColumn(): Attribute
    {   
        return Attribute::make(
            get: fn ($value) => $value,
        );
    }

(edit) or the last one, maybe you want to keep your column value clean, but you need to use another format from that column you can use something like this

public function getPrimaryColumnAnotherFormatAttribute()
    { 
	    // do something
        return $this->primary_column;
    }

Please or to participate in this conversation.