vendredi 16 novembre 2018

use sync method for an array and also pass additional pivot fields?

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire