Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hjortur17's avatar

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);
0 likes
6 replies
hjortur17's avatar

@MohamedTammam - This is the table structure:

public function up()
    {
        Schema::create('booking_information_pivot', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('booking_id');
            $table->unsignedBigInteger('car_id');
            $table->unsignedBigInteger('customer_id');
            $table->unsignedBigInteger('driver_id');

            $table->unique(['booking_id', 'car_id', 'customer_id', 'driver_id'], 'booking_info_unique');
        });

        Schema::table('booking_information_pivot', function($table) {
            $table->foreign('booking_id')->references('id')->on('bookings')->onDelete('cascade');
            $table->foreign('car_id')->references('id')->on('cars')->onDelete('cascade');
            $table->foreign('customer_id')->references('id')->on('customers')->onDelete('cascade');
            $table->foreign('driver_id')->references('id')->on('drivers')->onDelete('cascade');
        });
    }
MohamedTammam's avatar
Level 51

@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

1 like
hjortur17's avatar

@MohamedTammam - Thanks! That's solves it! But quick question, is it necessary to do also $booking->drivers()->attach(...) and $booking->car()->attach(...) because I'm already attaching them through the ->customers() relationship?

MohamedTammam's avatar

@hjortur17 When you call attach method, it set the relationship key value. If you set it before, then you don't need to call it. However, because you have different relationship key at the same table you need to add the value together because that keys aren't nullable.

Example

$booking->customers()->attach($customer, [
	'car_id' => $some_value,
	'driver_id' => $driver_id_value,
]);

With the above example, you don't need to call attach again for driver model.

1 like

Please or to participate in this conversation.