mavinoo / laravelBatch

insert batch and update batch in laravel
MIT License
564 stars 118 forks source link

how to update with pivot tables? #94

Open Nemachtilli opened 2 years ago

Nemachtilli commented 2 years ago

Hello this is the following update method:

`public function updateProfile(ProfileRequest $request, Profile $profile) { try { \DB::beginTransaction();

        $profile = new Profile;

        $input = [
            $this->profileInput(),
        ];

        $index = 'id';

        /** @var Mavinoo\Batch\Batch $batch */
        $batch = batch();
        $batch->update($profile, $input, $index);

        \DB::commit();
        session()->flash("message", ["success", __("<h5>Perfil actualizado</h5>")]);
        return redirect(route('profile', ['profile' => $profile]));
    } catch (\Throwable $exception) {
        \DB::rollBack();
        session()->flash("message", ["danger", $exception->getMessage()]);
        return back();
    }
}`

A new profile create, I have an array of a many to many relationship, for that relationship is saved in the database in the store method, i have:

$profile->hobbies()->sync(request("hobbies"));

putting that line of code in the update method gives me the following error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'profile_id' cannot be null (SQL: insert into category_profile (category_id, profile_id) values (1, ?))

how can i solve it?