vendredi 4 novembre 2022

How to store multiple checkbox values in Laravel 8

I'm new to Laravel and I made a survey form with multiple checkbox to store in the database, when the form submits it only reads the last input here is my Model for it :

    protected $fillable = ['Name',
                           'email',
                           'number',
                           'age',
                           'roles',
                           'recommend',
                           'options',
                           'comment'];
 protected $casts = ['options'=>'array'];

here is my Controller :

User::create($request->all());

    return redirect()->route('user.index')
    ->with('success','Info Added successfully');

Migration :

 $table->set ('options', ['Front-end Projects', 'Back-end Projects', 'Data Visualization', 'Challenges',
                     'Open Source Community', 'Gitter help rooms', 'Videos', 'City Meetups', 'Wiki', 'Forum', 'Additional Courses'
        ])->nullable();

and that's my Blade :

<input type="checkbox" name="options[]" value="Front-end Projects">Front-end Projects
  <br>
  <input type="checkbox" name="options[]" value="Back-end Projects">Back-end Projects
  <br>
  <input type="checkbox" name="options[]" value="Data Visualization">Data Visualization
  <br>
  <input type="checkbox" name="options[]" value="Challenges">Challenges
  <br>
  <input type="checkbox" name="options[]" value="Open Source Community">Open Source Community
  <br>
  <input type="checkbox" name="options[]" value="Gitter help rooms">Gitter help rooms
  <br>
  <input type="checkbox" name="options[]" value="Videos">Videos<br>
  <input type="checkbox" name="options[]" value="City Meetups">City Meetups<br>
  <input type="checkbox" name="options[]" value="Wiki">Wiki<br>
  <input type="checkbox" name="options[]" value="Forum">Forum<br>
  <input type="checkbox" name="options[]" value="Additional Courses">Additional Courses<br>

this what I tried and I got this error Error




Aucun commentaire:

Enregistrer un commentaire