Please show your tables structures.
Help with attach
Hi, I'm trying to attach a customer to a booking thorugh many-to-many relationship. I have a information pivot table which keep hold of connection between booking, customer, drivers and what car the customer selected. I have on my Booking model belongsToMany connection to all of the other models, like this:
public function customers()
{
return $this->belongsToMany(Customer::class, 'booking_information_pivot');
}
This is how I'm trying to attach this, in my store method I am creating both the booking and the customer and then when I try to attach the customer to the newly created booking, I get this error: SQLSTATE[HY000]: General error: 1364 Field 'car_id' doesn't have a default value. What is my best choose here? Should I split this all into separate pivot tables or does Laravel offer some other solution to this. Thanks in advance :)
This is how I'm trying to do this:
$customer = Customer::create([
'email_address' => $request->user['email'],
'phone_number' => $request->user['phone'],
'country' => $request->user['country'],
...
]);
$booking = Booking::create([
'pick_up_date' => $request->fromDateInfo['date'],
'pick_up_time' => $request->fromDateInfo['time'],
'drop_off_date' => $request->toDateInfo['date'],
'drop_off_time' => $request->toDateInfo['time'],
...
])
// Trying to attach the customer to the newly create booking
$booking->customers()->attach($customer);
@hjortur17 You need to add a value for car_id or make it nullable
$booking->customers()->attach($customer, ['car_id' => $some_value]);
OR
$table->unsignedBigInteger('car_id')->nullable();
I believe, you will also need to do that with driver_id
Please or to participate in this conversation.