The name of form element should be array (input name="checkers[]") then you can do:
$website->checkers()->sync(request('checkers'));
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying to insert a record in the associative table when I insert a new website. request comes from a vue.js form
Website Model
public function checkers(): BelongsToMany
{
return $this->belongsToMany(Checker::class)->withPivot('is_active');
}
Checker Model
public function websites(): BelongsToMany
{
return $this->belongsToMany(Website::class)->withPivot('is_active');
}
my request comes as an array of IDs that I have to attach to one item that just has been created. here is the codes I tried :
$website->checkers()->attach([request('checkers')]);
$website->checkers()->syncWithPivotValues([request('checkers')], ['is_active'=>'1']);
foreach(request('checkers', []) as $checker_id) {
$website->checkers()->attach([$checker_id]);
}
any help will be very appreciated
Please or to participate in this conversation.