Checkbox multiple store in database while in foreach loop

Saving existing student data in another table, how can I store multiple checkbox value while in foreach loop?

Hey, @Lawren

here is an example:

                    @foreach($permissions as $key => $permission)
                        <div class="">
                            <div class="">
                                <div>
                                    <label class="r">
                                        <input type="checkbox" class="" wire:model.lazy="permission" value="{{$permission->id}}" checked>
                                        <span class="">{{ $permission->name }}</span>
                                    </label>
                                </div>
                            </div>
                        </div>
                    @endforeach

In your component:

    /** @var array */
    public $permission = [];

and you can catch the values of the permission property directly when you submit the form

1 Like

In database? I have “70 slots” fields or what is the best option? Making multiple fields in multiple data or one field with relationship?

I get the array of selected id, how can I insert it in the database?

public $selectedStudents = [];
public $name;

public function create()
{
    StudentYearSection::create([
        'selectedStudents' => $this->name,
    ]);
}

It returns “Column ‘name’ cannot be null”…

Please help me sir @skywalker.

Hey, @Lawren

Sory for late response.

If you are dealing with many to many relationship. You can use sync method to deal with all the returned ids

$model->sync($ids)

or use insert

$data = [
    ['user_id'=>'Coder 1', 'subject_id'=> 4096],
    ['user_id'=>'Coder 2', 'subject_id'=> 2048],
    //...
];

Model::insert($data); // Eloquent approach
DB::table('table')->insert($data); // Query Builder approach

References:

2 Likes

Mr. @skywalker I have another problem with attaching records on many to many relationships,

public function create()
{
$validateEvaluation = $this->validate([
‘school_year’ => ‘required’,
‘semester’ => ‘required’,
‘instructor’ => ‘required’,
‘course’ => ‘required’,
‘subject_code’ => ‘required’,
‘year_and_section’ => ‘required’,
‘selectedStudents’ => ‘required’,
],[
‘selectedStudents.required’ => ‘This student number checkbox field is required.’
]);
$evaluator = Spe::create($validateEvaluation);
$this->resetValidation();
$this->emit(‘created’);
$evaluator->users()->attach($this->selectedStudents);
// dd($this->selectedStudents);
}

In the database, it returns duplicate data how can I avoid duplicates when I submitted once?

hey @Lawren

Use sync or syncWithoutDetaching instead of attach

1 Like

Thank you, sir. It worked! Last question, how to reset all fields after summiting the form. When I use $this->reset() the form submitted but in intermediate table no data inserted but when I removed the $this->reset it works again but the form is not resetting.

Just empty the fields manulay for example

$this->email = '';

// or 

$this->email['key'] = '';
1 Like

Again, thank you sir!

Glade to help!

Happy coding :slight_smile:

1 Like