本文介绍了完整性约束违规:1452 Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Laravel中创建了两个表,其中包含以下迁移:



用户迁移:

  public function up()
{
Schema :: create('users',function($ table){
$ table-> incremental('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()
{
模式:: create('occasionions',function($ table){
$ table-> ('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->整数('category') - > unsigned ();
$ table-> foreign('category') - > references('id') - > on('occasion_categories');
$ table-> string ',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->整数('几公里) - > nullable();
$ table-> decimal('price',8,2);
$ table-> decimal('action_price',8,2) - >可空();
$ table-> text('description') - > nullable();
});
}

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

'created_by_user_id'=> Auth :: id(),
' updated_by_user_id'=> Auth :: id(),
'title'=> Input :: get('title'),
'slug'=> Str :: slug get('title'),
'category'=> Input :: get('category'),
'brand'=> Input :: get('brand'),
'model'=> Input :: get('model'),
'type'=> Input :: get('type'),
'body'=>输入: :get('body'),
'color'=> Input :: get('color'),
'fuel'=> Input :: get('fuel'),
'transmission'=> Input :: get('transmission'),
'usage'=> Input :: get('usage'),
'engine_capacity'=>输入: :get('engine-capacity'),
'building_year'=> Input :: get('building-year'),
'sign'=> Input :: get sign'),
'mot'=> Input :: get('mot'),
'km'=> Input :: get('km'),
'price '=> Input :: get('price'),
'action_price'=> Input :: get('action-price'),
'description'=> Input :: get ('description')
));

我收到以下错误:

我在Google上搜索,但大多数人都说我收到这个错误,因为外键不存在, t true,因为当我尝试在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默认保护您的代码免受的限制:

您需要通过添加您的模型中的 $ fillable 列数组:

  class场合扩展口才
{
protected $ fillable = array(
'created_by_user_id',
'updated_by_user_id',
//您希望其余的列名称是质量可分。
);
}

或者做相反的事情,保护你不想要的任何列被大量分配:

  class场合扩展Eloquent 
{
protected $ guarded = array(
//任何你不想被大量分配的列
//或者只有空数组,如果所有的都是大量可分配的
);
}

请注意,您正在分配 Input :: get )直接进入模型。以下是部分的警告:


I have two tables in laravel created with the following migrations:

User migration:

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

Occasion migration:

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

Now when I try to create a Occasion with the following code:

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

I get the following error:

I searched on Google but most of the people say I get this error because the foreign key does not exists, but that isn't true, because when i try to add the same values in PhpMyAdmin it does work and the user id which i am inserting is 1 and it does exist.

解决方案

Your final sql query is insert into occasions (updated_at, created_at) values (2014-06-30 18:42:11, 2014-06-30 18:42:11). You should see that only updated_at and created_at is being inserted.

This happens because Laravel protects your code against against Mass Assignment by default:

You'll need to make your columns fillable by adding an array of $fillable columns in your model:

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

Or do the opposite, guard any columns that you don't want them 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.
    );
}

Note that you are assigning Input::get() into the model directly. Here is a caution from Mass Assignment section:

这篇关于完整性约束违规:1452 Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:10