You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel:如何通过Review模型适配create方法中的驼峰命名变量与蛇形命名数据库字段

Fixing CamelCase Request Fields to Snake_Case Database Columns in Laravel's create() Method

Absolutely! You can resolve this mismatch between your camelCase request parameters (userId, movieId) and snake_case database columns (user_id, movie_id) by making adjustments to your Review model. Here are a few straightforward approaches:

1. Leverage Laravel's Default Snake Attribute Conversion

Laravel's Eloquent has a built-in feature that automatically converts camelCase model attributes to snake_case database columns (this is enabled by default via the $snakeAttributes property, which defaults to true).

The key here is ensuring your model's $fillable array includes the snake_case database column names. Here's how your model should look:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Review extends Model
{
    // Allow mass assignment for your snake_case columns
    protected $fillable = ['user_id', 'movie_id', 'title', 'review'];
    
    // $snakeAttributes = true; // This is enabled by default, so you don't need to explicitly define it
}

When you call Review::create() with your camelCase validated data, Eloquent will automatically map userId to user_id and movieId to movie_id behind the scenes, while respecting your $fillable rules. This should resolve your SQL error immediately.

2. Override the fill() Method for Explicit Conversion

If for some reason the default conversion isn't working (e.g., older Laravel version, custom configuration), you can explicitly convert the request array's keys from camelCase to snake_case by overriding the model's fill() method:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Review extends Model
{
    protected $fillable = ['user_id', 'movie_id', 'title', 'review'];

    public function fill(array $attributes)
    {
        // Convert all camelCase keys to snake_case
        $snakeCaseAttributes = collect($attributes)->mapWithKeys(function ($value, $key) {
            return [Str::snake($key) => $value];
        })->toArray();

        // Pass the converted attributes to the parent fill method
        return parent::fill($snakeCaseAttributes);
    }
}

This method ensures every attribute key gets converted before mass assignment, so you can use camelCase or snake_case interchangeably in your create() call.

3. Define Attribute Mutators

For a more granular approach, you can create mutators for the camelCase attributes that map directly to your snake_case columns:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Review extends Model
{
    protected $fillable = ['user_id', 'movie_id', 'title', 'review'];

    // Mutator for userId -> maps to user_id
    public function setUserIdAttribute($value)
    {
        $this->attributes['user_id'] = $value;
    }

    // Mutator for movieId -> maps to movie_id
    public function setMovieIdAttribute($value)
    {
        $this->attributes['movie_id'] = $value;
    }
}

When you assign a value to $review->userId (or pass userId in the create() array), the mutator automatically stores it in the user_id database column.

All three methods will let you keep using camelCase in your request validation while playing nicely with your snake_case database schema.

内容的提问来源于stack exchange,提问作者Zoran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 10:57:29