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

tomekn's avatar

Edit form with checkboxes

I have a problem with the correct operation of the checkbox control when editing data.

If I use a code:

<input type="checkbox" class="form-checkbox" name="select_tags.{{ $tag->tag_id }}[]" wire:dirty="select_tags.{{ $tag->tag_id }}" value="{{ $tag->tag_id }}" {{ in_array($tag->tag_id, $cms_tags->pluck('tag_id')->toArray()) ? ' checked' : '' }}>

On the website I can see which checkbox-es have been selected earlier. (Checkboxes are marked) but when I change and save my array with the selected tags (checkboxes) is empty.

If I use a code:

<input type="checkbox" class="form-checkbox" name="select_tags.{{ $tag->tag_id }}[]" wire:model="select_tags.{{ $tag->tag_id }}" value="{{ $tag->tag_id }}" {{ in_array($tag->tag_id, $cms_tags->pluck('tag_id')->toArray()) ? ' checked' : '' }}>

I do not see on the website which boxes have been selected earlier. (Checkboxes are not marked) but when I change and save my array with the selected tags (checkboxes) is not empty and contains the id of the selected tags.

How to make the selection of checkboxes and their saving in the database work properly

0 likes
9 replies
jlrdw's avatar

If a checkbox is not checked nothing is passed.

tomekn's avatar

When editing the form, I select some checkboxes but array is [].

newbie360's avatar
name="select_tags.{{ $tag->tag_id }}[]"

for normal case, not need append .{{ $tag->tag_id }}, because value="{{ already contain the tag id }}"

view the html sorce code, you will see

name="select_tags.1[]"
name="select_tags.2[]"
name="select_tags.3[]"
name="select_tags.4[]"

so just use

name="select_tags[]"

and use ->sync() in backend update the data

tomekn's avatar

My view is:

<div class="form-group mt-2">
        <label class="block text-gray-700 text-sm font-bold mb-2">Tagi:</label>
        <div class="mt-2">
            @foreach ($tags as $key => $tag)
                <label class="flex items-center">
                    <input type="checkbox" class="form-checkbox" name="select_tags[]" wire:dirty="select_tags" value="{{ $tag->tag_id }}" {{ in_array($tag->tag_id, $cms_tags->pluck('tag_id')->toArray()) ? ' checked' : '' }}>
                    <span class="ml-2">{{ $tag->tag_name }}</span>
                </label>
            @endforeach
        </div>
        @error('tags') <span class="text-red-500">{{ $message }}</span>@enderror
    </div>

My Component is:

public function update()
    {
        $this->validate([
            'cms_title' => 'required|max:50|validate_name|unique:cmss,cms_title,'.$this->cms_id.',cms_id',
            'cms_content' => 'required',
            'cms_visible' => 'required',
        ]);

        if ($this->cms_id) {
            $record = Cms::find($this->cms_id);
            $record->update([
                'cms_title' => $this->cms_title,
                'cms_content' => $this->cms_content,
                'cms_visible' => $this->cms_visible,
            ]);

            if (count($this->select_tags) > 0) {
                CmsTag::where('cms_id', $this->cms_id)->delete();

                foreach ($this->select_tags as $key => $value) {
                    CmsTag::create([
                        'cms_id' => $this->cms_id,
                        'tag_id' => $value,
                    ]);
                }
            }
}

I corrected the tag but I am not sure how to use ->sync() you have mentioned.

newbie360's avatar

seems you are oneToMany, and not use any relationship to create, so your code work ?

but this line actually not correct

if (count($this->select_tags) > 0) {

for example 5 tag already exists in database, now in the view you un-check all 5 tag, you can't delete all the exists tag in database

the better way is compare the the (new_select_tags !== old_database_tags)

tomekn's avatar

I didn't implement oneToMany in my code model.

Yes you are right that the code:

if (count($this->select_tags) > 0) {

is bad. I changed this code.

I don't understand why one of the codes in my view works different.

I made a test: When I use code:

<input type="checkbox" class="form-checkbox" name="select_tags.{{ $tag->tag_id }}[]" wire:model="select_tags.{{ $tag->tag_id }}" value="{{ $tag->tag_id }}" {{ in_array($tag->tag_id, $cms_tags->pluck('tag_id')->toArray()) ? ' checked' : '' }}>

I don't see that the checkboxes are checked. But when I check in chrome developer I see:

<input type="checkbox" class="form-checkbox" name="select_tags[]" wire:model="select_tags" value="11" checked>

so it shows "checked".

What's interesting is that when I copy this line and paste it in another place there is no value for checked as you can see below:

<input type="checkbox" class="form-checkbox" name="select_tags[]" wire:model="select_tags" value="11" checked="">

but when I look at this in Chorme I see:

<input type="checkbox" class="form-checkbox" name="select_tags[]" wire:model="select_tags" value="11" checked>
newbie360's avatar

checked="" is equal checked

without checked means un-checked

may be i need upgrade to use Laravel 8, because i don't know about Livewire ;(

tomekn's avatar

You're right about checked

I use laravel ver 8 and everything is updated.

newbie360's avatar

hmm, i don't know how this done by Laravel 8 livewire, because i was still using Laravel 7

here is some code part of my project, and is very simple, you may got an idea

tags checkbox front-end code: http://os33.atwebpages.com/checkbox-button.html

blue color button means checked

Form Request:

public function rules()
{
    return [
        'tags' => 'required|array',
        'tags.*' => 'distinct|exists:tags,id',
    ];
}

Controller:

public function update(UpdateVideoTagRequest $request, Video $video)
{
    // many-to-many
    $video->tags()->sync($request->validated()['tags']);
    return redirect()->route('videos.edit', $video);
}

Please or to participate in this conversation.