Php 完整性约束冲突:1452 laravel
我在laravel中有两个表是通过以下迁移创建的: 用户迁移:Php 完整性约束冲突:1452 laravel,php,mysql,laravel,eloquent,relationship,Php,Mysql,Laravel,Eloquent,Relationship,我在laravel中有两个表是通过以下迁移创建的: 用户迁移: public function up() { Schema::create('users', function($table){ $table->increments('id')->unsigned(); $table->string('email')->unique(); $table->string('password', 64);
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', 8, 2)->nullable();
$table->text('description')->nullable();
});
}
迁移的时机:
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', 8, 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]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(henw
ocations
,约束ocations\u由用户创建的\u id\u外键(由用户创建的\u id
)引用users
(id
)(SQL:插入到场合(更新时间,创建时间)值(2014-06-30 18:42:112014-06-30 18:42:11))
我在谷歌上搜索过,但大多数人说我得到这个错误是因为外键不存在,但事实并非如此,因为当我尝试在PhpMyAdmin中添加相同的值时,它确实起作用,并且我插入的用户id是1,它确实存在。您的最终sql查询是插入到场景中(更新时,创建时)值(2014-06-30 18:42:112014-06-30 18:42:11)
。您应该看到只插入了在
处更新的和在
处创建的
发生这种情况的原因是,默认情况下,Laravel保护您的代码不受以下因素的影响:
如果用户输入被盲目地传递到模型中,用户可以自由修改模型的任何和所有属性。因此,默认情况下,所有有说服力的模型都可以防止大规模分配
您需要在模型中添加一组$filleble
列,以使列可填充:
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()
直接分配到模型中。下面是第节中的注意事项:
注意:在使用受保护的方法时,仍然不应将Input::get()或用户控制输入的任何原始数组传递到save或update方法中,因为任何未受保护的列都可能被更新
谢谢!我有一个数组filleble
填充了所有属性的名称,现在它可以工作了!但是这样做安全吗?好的,你将受到SQL注入的保护。但是你仍然需要自己进行输入验证,以确保数据有效。我有相同的错误消息,只是它对我的读取方式不同,因为我有不同的表。我怎样才能读出错误字符串,哪些字段被故意保护?我有点不知道问题到底出在哪里。o.o。。。