Integrity constraint violation: 1452 laravel

user3527894

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:

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

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.

Unnawut

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:

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.

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:

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Integrity constraint violation: 1452 laravel

From Dev

Integrity constraint violation: 1452 fillable also not worked

From Dev

Laravel : Integrity constraint violation

From Dev

Integrity constraint violation in laravel

From Dev

Integrity constraint violation in laravel

From Dev

laravel Integrity constraint violation

From Dev

Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel

From Dev

Laravel - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel

From Dev

Laravel: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - laravel

From Dev

Laravel Seed: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

Laravel - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

Laravel: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

Laravel SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row

From Dev

Laravel SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row (laravel5)

From Dev

Laravel validator - Integrity constraint violation

From Dev

Laravel validator - Integrity constraint violation

From Dev

Laravel 5: SQLSTATE[23000]: Integrity constraint violation

From Dev

Integrity constraint violation - PHP Laravel MVC 5.0

From Dev

Laravel - Integrity constraint violation: 1062 Duplicate entry

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

From Dev

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:a foreign key

Related Related

  1. 1

    Integrity constraint violation: 1452 laravel

  2. 2

    Integrity constraint violation: 1452 fillable also not worked

  3. 3

    Laravel : Integrity constraint violation

  4. 4

    Integrity constraint violation in laravel

  5. 5

    Integrity constraint violation in laravel

  6. 6

    laravel Integrity constraint violation

  7. 7

    Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  8. 8

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel

  9. 9

    Laravel - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  10. 10

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel

  11. 11

    Laravel: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  12. 12

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - laravel

  13. 13

    Laravel Seed: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  14. 14

    Laravel - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  15. 15

    Laravel: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  16. 16

    Laravel SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row

  17. 17

    Laravel SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row

  18. 18

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row (laravel5)

  19. 19

    Laravel validator - Integrity constraint violation

  20. 20

    Laravel validator - Integrity constraint violation

  21. 21

    Laravel 5: SQLSTATE[23000]: Integrity constraint violation

  22. 22

    Integrity constraint violation - PHP Laravel MVC 5.0

  23. 23

    Laravel - Integrity constraint violation: 1062 Duplicate entry

  24. 24

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  25. 25

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  26. 26

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  27. 27

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  28. 28

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

  29. 29

    SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:a foreign key

HotTag

Archive