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

ZeroGodForce's avatar

Update 2 tables with multiselect - sync not working

Hi All,

I have a many to many relationship where I'm trying to attach multiple 'clients' to one booking using a multiselect dropdown. So far (after a lot of grief), I managed to write the data into the pivot table and get it back out again, but I can't get it to update the record (i.e. add or remove clients from the booking).

(edited for brevity)

Booking.php

class Booking extends Model
{
    public function clients()
    {
        return $this->belongsToMany('FitnessKookie\Models\Client')->withTimestamps();
    }
    public function getClientListAttribute()
    {
        return $this->clients->lists('id')->toArray();
    }
}

BookingsController.php

    public function update(Request $request, Booking $booking)
    {
        $booking = $request->all();

        $booking->session_date = Carbon::createFromFormat('d/m/Y', $request->session_date);
        $booking->start_time = Carbon::createFromFormat('h:i A', $request->start_time);
        $booking->finish_time = Carbon::createFromFormat('h:i A', $request->finish_time);

        $booking->clients()->sync($request->input('client_list'));
        $booking->save();

        return $booking;
    }

edit.blade.php

    {!! Form::model($booking, ['method' => 'PATCH', 'action' => ['BookingsController@update', $booking->id]]) !!}

    {!! Form::label('client_list', 'Clients:') !!}
    {!! Form::select('client_list[]', $clients, $booking->client_list, ['class' => 'form-control', 'multiple']) !!}

In my various attempts to get this working, I've had it add new records instead of modifing existing, got "Attempt to assign property of non-object errors", or else nothing at all.

I've tried following Jeffery's fundamentals tutorial on syncing (the one with the tags), but for reasons I just can't explain I can't get that method to work, and it's really starting to infuriate me. If anyone could help I'd be super grateful as I'm mere hours away from punching my computer in the face.

0 likes
2 replies
julian's avatar

Does $request->input('client_list') contain the Clients IDs? ['1','2'] or names?

Please or to participate in this conversation.