违反完整性约束:1452 laravel

use*_*894 5 php mysql relationship laravel eloquent

我通过以下迁移创建了两个laravel表:

用户迁移:

public function up()
{
    Schema::create('users', function($table){
        $table->increments('id')->unsigned();
        $table->string('email')->unique();
        $table->string('password', 64);
        $table->string('first_name', 32);
        $table->string('last_name', 32);
        $table->string('remember_token', 100)->nullable();
    });
}
Run Code Online (Sandbox Code Playgroud)

场合迁移:

public function up()
{
    Schema::create('occasions', function($table){
        $table->increments('id')->unsigned();
        $table->integer('created_by_user_id')->unsigned();
        $table->foreign('created_by_user_id')->references('id')->on('users');
        $table->integer('updated_by_user_id')->unsigned();
        $table->foreign('updated_by_user_id')->references('id')->on('users');
        $table->timestamps();
        $table->string('title')->unique();
        $table->string('slug')->unique();
        $table->integer('category')->unsigned();
        $table->foreign('category')->references('id')->on('occasion_categories');
        $table->string('brand', 32);
        $table->string('model', 32)->nullable();
        $table->string('type', 32)->nullable();
        $table->string('body', 32)->nullable();
        $table->string('color', 32);
        $table->integer('fuel')->unsigned()->nullable();
        $table->foreign('fuel')->references('id')->on('occasion_fuels');
        $table->integer('transmission')->unsigned()->nullable();
        $table->foreign('transmission')->references('id')->on('occasion_transmissions');
        $table->decimal('usage', 6, 2)->nullable();
        $table->integer('engine_capacity')->unsigned()->nullable();
        $table->integer('building_year')->unsigned()->nullable();
        $table->string('sign', 8)->nullable();
        $table->date('mot')->nullable();
        $table->integer('kilometers')->nullable();
        $table->decimal('price', 8, 2);
        $table->decimal('action_price', 8, 2)->nullable();
        $table->text('description')->nullable();
    });
}
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试使用以下代码创建一个场合时:

Occasion::create(array(
    'created_by_user_id'=>Auth::id(),
    'updated_by_user_id'=>Auth::id(),
    'title'=>Input::get('title'),
    'slug'=>Str::slug(Input::get('title')),
    'category'=>Input::get('category'),
    'brand'=>Input::get('brand'),
    'model'=>Input::get('model'),
    'type'=>Input::get('type'),
    'body'=>Input::get('body'),
    'color'=>Input::get('color'),
    'fuel'=>Input::get('fuel'),
    'transmission'=>Input::get('transmission'),
    'usage'=>Input::get('usage'),
    'engine_capacity'=>Input::get('engine-capacity'),
    'building_year'=>Input::get('building-year'),
    'sign'=>Input::get('sign'),
    'mot'=>Input::get('mot'),
    'kilometers'=>Input::get('kilometers'),
    'price'=>Input::get('price'),
    'action_price'=>Input::get('action-price'),
    'description'=>Input::get('description')
));
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束失败(henw.occasions,CONSTRAINT occasions_created_by_user_id_foreignFOREIGN KEY(created_by_user_id)REFERENCES users(id))(SQL:插入occasions(updated_at,created_at)值(2014-06-30) 18:42:11,2014-06-30 18:42:11))

我在Google上搜索过,但是大多数人都说我得到了这个错误,因为外键不存在,但事实并非如此,因为当我尝试在PhpMyAdmin中添加相同的值时,它确实起作用,并且用户ID是我的插入是1,它确实存在.

Unn*_*wut 5

您最终的sql查询为insert into occasions (updated_at, created_at) values (2014-06-30 18:42:11, 2014-06-30 18:42:11)。您应该只能看到updated_at并且created_at正在插入。

发生这种情况是因为默认情况下,Laravel保护您的代码免受批量分配

如果盲目地将用户输入传递给模型,则用户可以自由修改任何和所有模型的属性。因此,默认情况下,所有Eloquent模型都可以防止大规模分配。

您需要通过$fillable在模型中添加一个列数组来使列可填充:

class Occasion extends Eloquent
{
    protected $fillable = array(
        'created_by_user_id',
        'updated_by_user_id',
        // The rest of the column names that you want it to be mass-assignable.
    );
}
Run Code Online (Sandbox Code Playgroud)

或相反,保护您不希望它们可大规模分配的所有列:

class Occasion extends Eloquent
{
    protected $guarded = array(
        // Any columns you don't want to be mass-assignable.
        // Or just empty array if all is mass-assignable.
    );
}
Run Code Online (Sandbox Code Playgroud)

请注意,您是Input::get()直接分配给模型。这是“ 批量分配”部分的警告:

注意:使用受保护的方法时,仍应永远不要将Input :: get()或用户控制输入的任何原始数组传递到保存或更新方法中,因为任何不受保护的列都可能会更新。