php – 完整性约束违规:1452 laravel

我通过以下迁移创建了两个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();
    });
}

场合迁移:

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',2)->nullable();
        $table->text('description')->nullable();
    });
}

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

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')
));

我收到以下错误

sqlSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (henw.occasions,CONSTRAINT occasions_created_by_user_id_foreign FOREIGN KEY (created_by_user_id) REFERENCES users (id)) (sql: insert into occasions (updated_at,created_at) values (2014-06-30 18:42:11,2014-06-30 18:42:11))

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

最佳答案
您的最终SQL查询是插入到的场合(updated_at,created_at)值(2014-06-30 18:42:11,2014-06-30 18:42:11).您应该看到只插入了updated_at和created_at.

发生这种情况是因为Laravel默认保护您的代码不受Mass Assignment的影响:

If user input is blindly passed into a model,the user is free to modify any and all of the model’s attributes. For this reason,all Eloquent models protect against mass-assignment by default.

您需要通过在模型中添加$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.
    );
}

或者相反,保护您不希望它们可以批量分配的任何列:

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.
    );
}

请注意,您将直接将Input :: get()分配给模型.以下是Mass Assignment部分的警告:

Note: When using guarded,you should still never pass Input::get() or any raw array of user controlled input into a save or update method,as any column that is not guarded may be updated.

相关文章

昨天的考试过程中,有个考点的服务器蓝屏重启后发现Mysql启动不了(5.6.45 x32版本,使用innoDB),重装后...
整数类型 标准 SQL 中支持 INTEGER 和 SMALLINT 这两种类型,MySQL 数据库除了支持这两种类型以外,还扩...
一条 SQL 查询语句结构如下: SELECT DISTINCT <select_list> FROM <left_table&...
数据备份 1. 备份数据库 使用 mysqldump 命令可以将数据库中的数据备份成一个文本文件,表的结构和数据...
概述 在实际工作中,在关系数据库(MySQL、PostgreSQL)的单表数据量上亿后,往往会出现查询和分析变慢...
概述 触发器是 MySQL 的数据库对象之一,不需要程序调用或手工启动,而是由事件来触发、激活,从而实现...