Reserved column names in Eloquent
Asked Answered
C

2

9

From a cursory look into Illuminate\Database\Eloquent\Model I can see the following instance attributes:

protected $connection
protected $table
protected $primaryKey
protected $perPage
public    $incrementing
public    $timestamps
protected $attributes
protected $original
protected $relations
protected $hidden
protected $visible
protected $appends
protected $fillable
protected $guarded
protected $dates
protected $dateFormat
protected $casts
protected $touches
protected $observables
protected $with
protected $morphClass
public    $exists
public    $wasRecentlyCreated

Questions:

  • Why are these things not static, seeing as they are class-level configuration stuff?
  • Does it mean I cannot use these names for my table columns?
  • Is there an official list of names one cannot use as table columns?
  • What if I have a legacy table with columns named like this?
  • Who came up with this genius idea?
Ceruse answered 23/5, 2016 at 17:7 Comment(0)
B
1

These attributes can not be static because they are used as their model configuration. If you rewrites them in your model class, you define your own parameters, if not, the Eloquent believes it should use the defaults. Yes, you should not have a column name in your table that matches the name of one of these attributes.

Beseem answered 26/5, 2016 at 2:58 Comment(4)
Is the list of $hidden fields (for example) going to be different between two records of the same type? I don't think so. Therefore they should be static.Ceruse
Here is a issue opened on github which appears to say the same thing. github.com/laravel/framework/issues/15394 Basically "don't do it - it's an eloquent limitation". A pretty big limitation if you ask me.Chrystal
@Chris Pretty big limitation and only the tip of the iceberg. I stopped using Eloquent altogether. I use the DB facade, hand-written queries, and hand-written SQL migrations, which give me much more control.Ceruse
You can use them as column names, but you have to use getAttribute() and setAttribute() so I have a column 'original' on a model from a legacy system we need to keep as-is for now and you can $thing->setAttribute('original', 'value') $thing->getAttribute('original') but this is obviously more annoying than $thing->original = 'value' and $thing->original so avoid if possibleOversee
A
2

It appears that $changes is also a reserved name.

Advert answered 10/10, 2017 at 19:35 Comment(1)
This is true since Laravel 5.5.Meill
B
1

These attributes can not be static because they are used as their model configuration. If you rewrites them in your model class, you define your own parameters, if not, the Eloquent believes it should use the defaults. Yes, you should not have a column name in your table that matches the name of one of these attributes.

Beseem answered 26/5, 2016 at 2:58 Comment(4)
Is the list of $hidden fields (for example) going to be different between two records of the same type? I don't think so. Therefore they should be static.Ceruse
Here is a issue opened on github which appears to say the same thing. github.com/laravel/framework/issues/15394 Basically "don't do it - it's an eloquent limitation". A pretty big limitation if you ask me.Chrystal
@Chris Pretty big limitation and only the tip of the iceberg. I stopped using Eloquent altogether. I use the DB facade, hand-written queries, and hand-written SQL migrations, which give me much more control.Ceruse
You can use them as column names, but you have to use getAttribute() and setAttribute() so I have a column 'original' on a model from a legacy system we need to keep as-is for now and you can $thing->setAttribute('original', 'value') $thing->getAttribute('original') but this is obviously more annoying than $thing->original = 'value' and $thing->original so avoid if possibleOversee

© 2022 - 2024 — McMap. All rights reserved.