Is $contact a model that is stored in the database or an ID of it, because it should be AFAIK.
For more info: https://laravel.com/docs/8.x/eloquent-relationships#updating-many-to-many-relationships
Oct 4, 2021
6
Level 5
Attach not working for many-to-many.
$order->contacts()->attach($contact, ['type' => $request->contact_type]);
It used to work, but not anymore. I get "order_id" doesn't have a default value.
'order_id' is fillable.
Full error:
1364 Field 'order_id' doesn't have a default value (SQL: insert into `salesorder_contacts` (`contact_id`, `type`) values (359, Resolution))"
Level 5
I figured it out. In the model, I had to add the foreign pivot id because the table is named something different.
public function contacts()
{
return $this->belongsToMany('App\Models\Contact\Contact', 'salesorder_contacts', 'contact_id', 'order_id')->withPivot('type');
}
Please or to participate in this conversation.