我在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', 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
。occasions
,CONSTRAINToccasions_created_by_user_id_foreign
FOREIGN KEY({{ 1}})REFERENCEScreated_by_user_id
(users
))(SQL:插入id
(occasions
,updated_at
)值(2014-06-30 18: 42:11,2014-06-30 18:42:11))
我在Google上搜索过,但大多数人都说我收到此错误,因为外键不存在,但事实并非如此,因为当我尝试在PhpMyAdmin中添加相同的值时,它确实起作用并且用户ID我插入的是1,它确实存在。
答案 0 :(得分: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默认保护您的代码免受Mass Assignment的攻击:
如果用户输入被盲目地传递到模型中,则用户可以自由地修改任何和所有模型的属性。因此,默认情况下,所有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.
);
}
或者相反,保护您不希望它们可以批量分配的任何列:
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部分的注意事项:
注意:使用guarded时,您仍然不应该将Input :: get()或任何原始用户控制输入数组传递给save或update方法,因为任何未受保护的列都可能会被更新。