How to force laravel to use camelCase for column names

Larvel uses the name snake_case for the name of the table columns. How to rewrite this assumption as the next getter works?

public function getFooBarAttribute()
{
    return json_decode($this->attributes['fooBar'], true);
}
+4
source share
1 answer

I need to caution this answer first with the following: I have not tested this code; secondly, I never had a requirement to use this.

With that said, there is a static property available on eloquent models:

/**
 * Indicates whether attributes are snake cased on arrays.
 *
 * @var bool
 */
public static $snakeAttributes = true;

By switching this to false, you disable the snake shell with the attribute names and relationship names on the model. This should have the desired result you are looking for.

, cacheMutatedAttributes, , , , , .

if (static::$snakeAttributes) {
    $match = Str::snake($match);
}

, , . , , . , - AppServiceProvider:

public function boot()
{
    \Illuminate\Database\Eloquent\Model::$snakeAttributes = false;
}
+7

Source: https://habr.com/ru/post/1628694/


All Articles