1

This is my input array

"events" => array:2 [▼
"special-date" => array:3 [▼
  0 => "14-Nov-1979"
  1 => "18-Apr-1981"
  2 => "12-Nov-1978"
]
"event" => array:3 [▼
  0 => "2"
  1 => "3"
  2 => "4"
]

]

This is my code

$User->events()->sync($request->events['event'], ['event_date' => $request->events['special-date']]);

here, $user is an instance of the user model

This is my user model realtion function

 public function events()
{
    return $this->belongsToMany('App\Models\Event', 'user_events', 'user_id', 'event_id')->withPivot('event_date');
}

and I got an error of event_date has no default value.

my table structure of a user_event table is

id, user_id, event_id, event_date

Ankita
  • 340
  • 2
  • 8
  • Possible duplicate of [Laravel, sync() - how to sync an array and also pass additional pivot fields?](https://stackoverflow.com/questions/27230672/laravel-sync-how-to-sync-an-array-and-also-pass-additional-pivot-fields) – Remul Nov 16 '18 at 12:08
  • Yes, but I can't get the desired solution, I got above error. – Ankita Nov 16 '18 at 12:33
  • You are passing two arrays with data to the sync method, you have to pass it in one array. The accepted answer in the linked question explains it. – Remul Nov 16 '18 at 13:19

0 Answers0