-3

chinese description:

laravel orm的命名规范对强迫症来说简直是一种灾难,为什么会有这样的设计。。

数据库字段设计成小写字母+下划线,例如order_item表中有个字段goods_name

那么laravel代码中,就得这样写:$orderItem->goods_name

驼峰和全小写混合,强迫症表示实在受不了啊。。。受不了。。。受不了。。。

english description: for example: I have a table named order_item and the table have a column named goods_name in laravel orm I will write code such as: $orderItem->goods_name

the hump naming with the whole lower case,,,,

why why why

why the laravel design so。why not design like:$orderItem->goodsName

I am going nuts.

  • uhh, this question is not exactly something you want to drop to stackoverflow. please contact Taylor Otwell himself or just bug people on [#laravel in freenode](https://webchat.freenode.net/). regarding the `$orderItem->goods_name`, you need to know the [case sensitivity on php](https://stackoverflow.com/questions/33273941/php-case-sensitivity) itself also... lol. – Bagus Tesa Apr 09 '19 at 02:55

1 Answers1

1

If you have a column in your database name as snake_case you probably have the same case name in your object model.

What you could do is to modify your OrderItem model to define an assessor and append a new variable like this

namespace App;

use Illuminate\Database\Eloquent\Model;

class OrderItem extends Model
{
    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['goodsName'];

    /**
     * Get the administrator flag for the user.
     *
     * @return bool
     */
    public function getGoodsNameAttribute()
    {
        return $this->attributes['goods_name'];
    }
}

/////
$model = OrderItem ::find(1);
dump($model->goodsName);
dd($model);
Ezequiel Fernandez
  • 954
  • 11
  • 18