0

Before you start giving vote-down or mark as duplicated please read:

I have read Cannot add or update a child row a foreign key constraint fails and it is not my case,

  1. I have existing record
  2. my table supports cascade on delete.
  3. I also have my unsigned columns nullable

-- codes provided below

Issue

I am trying save related data in 3rd table named product_attributes with sync method, which gets product_id and attribute_id

Code

schema

public function up()
{
        Schema::create('product_attributes', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('product_id')->nullable()->unsigned();
            $table->integer('attribute_id')->nullable()->unsigned();
        });
        Schema::table('product_attributes', function (Blueprint $table) {
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->foreign('attribute_id')->references('id')->on('attributes')->onDelete('cascade');
        });
}

product model

public function attributes(){
        return $this->belongsToMany(Attribute::class, 'product_attributes', 'product_id', 'attribute_id');
}

attribute model

public function products(){
        return $this->belongsToMany(Product::class);
}

product controller

$product->save()
$product->attributes()->sync($request->attributes, false);

blade

<select class="form-control" name="attributes[]" id="attributes" multiple="multiple">
  @foreach($attributes as $attribute)
    <option value="{{ $attribute->id }}">{{ $attribute->title }}</option>
  @endforeach
</select>

Error

this is what I'm getting:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`shopping`.`product_attributes`, CONSTRAINT `product_attributes_attribute_id_foreign` FOREIGN KEY (`attribute_id`) REFERENCES `attributes` (`id`) ON DELETE CASCADE) (SQL: insert into `product_attributes` (`attribute_id`, `product_id`) values (*parameters, 3))

Anyone can help with that?

Update

Here is what I get when I send my data to controller

one

mafortis
  • 6,750
  • 23
  • 130
  • 288
  • Seems about right. Except for your `->products()` relationship, but that shouldn't matter in the code you posted. I'd double check the value of `$request->attributes` just before the sync: `dd($request->attributes)` Seems like it's "*parameters", which just seems weird – DevK Jan 03 '19 at 06:18
  • @devk exactly, when i dd request->attributes it gives me empty parameters but you see in my dd request->all() i have those values. – mafortis Jan 03 '19 at 06:38
  • 1
    @mafortis try with `$request->get('attributes')` – Jinal Somaiya Jan 03 '19 at 06:40
  • @JinalSomaiya it's working now, thank you. But why isn't working in default code? the code i shared in my question I used in 4 applications before this is the first time i have to use it with get('')?! – mafortis Jan 03 '19 at 06:48
  • @mafortis read this for more info. https://laraveldaily.com/differences-request-get-vs-request-input-vs-helper-vs-get_data/ – Jinal Somaiya Jan 03 '19 at 06:57

0 Answers0