Laravel Eloquent 无法读取数据库表格中以数字作为列名的数据。

huangapple go评论68阅读模式
英文:

Laravel Eloquent unable to read data from databse table columns with numerical column names

问题

I have a mysql table used for a Laravel model. The table contains some columns whose names are purely numeric, i.e. contain only numbers which are years. Please see image attached below.

Laravel Eloquent 无法读取数据库表格中以数字作为列名的数据。

When I query the table using eloquent the result results returned are missing the numerical column names. Even though the values are returned, the column names are deformed and display as single digit numbers.

Laravel Eloquent 无法读取数据库表格中以数字作为列名的数据。

When using accessors, I'm able to return the columns with friendly names however I am not able to update the model columns.

So my questions are

  1. What is the best way to read and update the columns?
  2. I'm not able to change the column names so the only option is to find a work around in my laravel code.
英文:

I have a mysql table used for a Laravel model. The table contains some columns whose names are purely numeric, i.e. contain only numbers which are years. Please see image attached below.

Laravel Eloquent 无法读取数据库表格中以数字作为列名的数据。

When I query the table using eloquent the result results returned are missing the numerical column names. Even though the values are returned, the column names are deformed and display as single digit numbers.

Laravel Eloquent 无法读取数据库表格中以数字作为列名的数据。

When using accessors, I'm able to return the columns with friendly names however I am not able to update the model columns.

So my questions are

  1. What is the best way to read and update the columns?
  2. I'm not able to change the column names so the only option is to find a work around in my laravel code.

答案1

得分: 2

I guess this is because you can't name PHP variables with names starting with a number.

So to update the value, you will need to write setters in addition to your getters. You should be able to do that with Eloquent accessors and mutators.

Link to Laravel Documentation

Edit: After some tests, I didn't find how to do it with accessors and mutators, but I came up with another solution that might work, given Tim Lewis's comment.

public function setYear($yearColumnName, $value)
{
    $this->{$yearColumnName} = $value;
}
英文:

I guess this is because you can't name php variables with names starting with a number.

So to update the value you will need to write setters in addition to your getters. You should be able to do that with Eloquent accessors and mutators.

https://laravel.com/docs/10.x/eloquent-mutators#defining-a-mutator

Edit: After some test, I didn't find how to do it with accessors and mutators, but I came up with another solution who might be working given Tim Lewis's comment.

public function setYear($yearColumnName, $value)
{
    $this->{$yearColumnName} = $value;
}

huangapple
  • 本文由 发表于 2023年4月19日 21:25:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定