How can I save a relationship entry to a pivot table in laravel? <p>I have created some relationships where 2 models work together, currently they are <code>Offer</code> and <code>OfferDay</code>. I have created 2 tables one for Offers and one for OfferDays. Here are the migrations:</p> <p>offers</p> <pre><code>class CreateOffersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('offers', function(Blueprint $table) { $table-&gt;increments('id'); $table-&gt;string('offer_title'); $table-&gt;string('offer headline'); $table-&gt;string('offer_subheader'); $table-&gt;string('image'); $table-&gt;text('offer_terms'); $table-&gt;string('featured_date'); $table-&gt;boolean('is_featured'); $table-&gt;string('distance_to_offer'); $table-&gt;string('offer_end_time'); $table-&gt;string('offer_start_time'); $table-&gt;timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('offers'); } } </code></pre> <p>I then have a migration that runs to create the days for the relationship to work and this holds the day name and the day id in the table where I have seeded the database with days already. Here's the migration schema:</p> <p>offer_days</p> <pre><code>public function up() { Schema::create('offer_days', function(Blueprint $table) { $table-&gt;increments('id'); $table-&gt;string('day'); $table-&gt;timestamps(); }); } </code></pre> <p>This forms the basis of the relationships and the data we have got. The next bit is making them work and when adding an offer making sure I can populate the pivot table with the correct data that relates to offers and offer_days. Below are the 2 models I have got setup with all the correct <code>$fillable</code> attributes for the relationships to work:</p> <p>Offer</p> <pre><code> protected $fillable = ['id','venue_id','offer_day_id','offer_id','day','venue_address','is_featured','repeater_days','image', 'venue_description', 'offer_headline','offer_subheader','offer_terms','venue_phone_number','venue_website', 'venue_name', 'venue_headline','venue_description','venue_latitude','venue_longitude','days','offer_when','offer_end_time','offer_start_time','offer_start_date','offer_end_date','featured_date','current_time','current_date']; /** * Get the offers for the offer. */ public function offerdays() { return $this-&gt;belongsToMany('App\OfferDay', 'offer_day_offer', 'offer_day_id', 'offer_id'); } </code></pre> <p>OfferDay</p> <pre><code>protected $fillable = ['offer_id','offer_day_id']; /** * Get the offer days for the offer. */ public function offers() { //return $this-&gt;belongsToMany('App\Offer'); return $this-&gt;belongsToMany('App\Offer', 'offer_day_offer', 'offer_id', 'offer_day_id'); } </code></pre> <p>I have then created the pivot tables to correspond with this like so:</p> <pre><code>class CreateOfferDayOfferPivotTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('offer_day_offer', function (Blueprint $table) { $table-&gt;integer('offer_day_id')-&gt;unsigned()-&gt;index(); // $table-&gt;foreign('offer_day_id')-&gt;references('id')-&gt;on('offer_days')-&gt;onDelete('cascade'); $table-&gt;integer('offer_id')-&gt;unsigned()-&gt;index(); // $table-&gt;foreign('offer_id')-&gt;references('id')-&gt;on('offers')-&gt;onDelete('cascade'); $table-&gt;primary(['offer_day_id', 'offer_id']); }); Schema::table('offer_day_offer', function($table) { $table-&gt;foreign('offer_id')-&gt;references('id')-&gt;on('offers'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('offer_day_offer'); } } </code></pre> <p>In my controller I have setup the save method which should work like so:</p> <pre><code>if ($validation-&gt;fails()) { return redirect('offers')-&gt;with('message', $validation-&gt;errors()); } else { $file = Input::file('image'); $filename = date('Y-m-d-H')."-".$file-&gt;getClientOriginalName(); $path = public_path('images/offers/' . $filename); Image::make($file-&gt;getRealPath()) -&gt;save($path); $venue = Input::get('venue_id'); $day = Input::get('day'); $data['image'] = url('/') . '/' . 'images/offers/'.$filename; if($days){ $d = implode(',', $days); $data['days'] = $d; } $data['venue_id'] = $venue; $data['day'] = $day; $offer-&gt;offerdays()-&gt;attach(['offer_day_id' =&gt; 13, 'offer_id' =&gt; 530]); Offer::create( $data ); return redirect('offers')-&gt;with('message', 'Offer added!'); } </code></pre> <p>This <code>$offer-&gt;offerdays()-&gt;attach(['offer_day_id' =&gt; 13, 'offer_id' =&gt; 530]);</code> is the important bit but for some reason I am getting this error below:</p> <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`homestead`.`offer_day_offer`, CONSTRAINT `offer_day_offer_offer_id_foreign` FOREIGN KEY (`offer_id`) REFERENCES `offers` (`id`)) (SQL: insert into `offer_day_offer` (`offer_day_id`, `offer_id`) values (, 13), (, 530)) </code></pre> <p>Can someone try and shed light onto this?</p> <p>Thanks!</p>