vendredi 29 janvier 2021

How to Send Multiple Checkbox through migration in LARAVEL_7

I want to store the selected checks in a single variable and send it to the database, I am only using migrations, others that other data is sent in the migration.

Pd: I get this error by the array: ErrorException Array to string conversion

My template.

<form action="" method="POST">
                @csrf

                <div class="row">
                    <div class="col-md-6 mt-5">
                        <select name="Sede" class="form-select" aria-label="Default select example">
                            <option selected name="Sede">Campus Sede</option>
                            <option value="Cancún">Cancún</option>
                            <option value="Chihuahua">Chihuahua</option>
                            
                        </select>
                    </div>

                    <div class="col-md-6 mt-5">
                        <select name="Alcance" class="form-select" aria-label="Default select example">
                            <option selected name>Alcance</option>
                            <option value="Local">Local</option>
                            <option value="Nacional">Nacional</option>
                        </select>
                    </div>

                    <div class="col-md-6 mt-5">
                        <label for="">Nombre del Proyecto</label>
                        <input type="text" name="Nombre" class="form-control col-12">
                    </div>

                    <div class="col-md-6 mt-5">
                        <label for="">Cierre de Inscripciones</label>
                        <input data-date-format="yyyy/mm/dd" id="datepicker" name="Cierre" class="form-control col-12">
               
               
                    </div>

                    <div class="col-md-6 mt-5">
                        <input type="checkbox" name="Areas[]"id="ingenieria" value="Ingeniería" />
                        <label for="ingenieria">Ingeniería</label>
                        <input type="checkbox" name="Areas[]"id="humanidades" value="Humanidades" />
                        <label for="humanidades">Humanidades</label>
                        <input type="checkbox" name="Areas[]"id="negocios" value="Negocios" />
                        <label for="negocios">Negocios</label>
                        <input type="checkbox" name="Areas[]" id="salud" value="Salud" />
                        <label for="salud">Salud</label>
                    </div>

                    <div class="col-md-12 mt-5">
                        <label for="">Cual es el departamento donde impactara directamente el proyecto?</label>
                        <input type="text" name="P1" class="form-control col-12">
                    </div>

                    
                    <div class="row form-group mt-5">
                        <button type="submit" class="btn btn-success col-md-2 offset-5">Guardar</button>
                    </div>
            </form>

My controller:

public function save(Request $request){

        $validator = $this->validate($request, [
            
            'Sede'=> 'required|string|max:255',
            'Alcance'=> 'required|string|max:255',
            'Nombre'=> 'required|string|max:255',
            'Cierre'=> 'required|date',
            'Areas'=> 'required',
            'P1'=> 'required|string|max:255',
            'P2'=> 'required|string|max:255',
            'P3'=> 'required|string|max:255',
            'P4'=> 'required|string|max:255'
        ]);

       $data = request()->except('_token');

      Project::insert($data);
        
       return back()->with('datosGuardados', 'Datos almacenados, Correctamente');
   
      

    }

My Model:

class Project extends Model
{
    protected $fillable = ['Sede','Alcance','Nombre','Cierre','Areas','P1','P2','P3','P4'];

    protected $casts = [
        'Areas' => 'array'
    ];
}

My migration:

public function up()
{
    Schema::create('projects', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('Sede');
        $table->string('Alcance');
        $table->string('Nombre');
        $table->date('Cierre');
        $table->string('Areas');
        $table->string('P1');
        $table->string('P2');
        $table->string('P3');
        $table->string('P4');
        $table->timestamps();
    });
}

enter image description here

As you see the error is due to the checkbox that I don't know how to send it as a single array and store it in the database, I would appreciate any help. I was investigating and I did not find anything




Aucun commentaire:

Enregistrer un commentaire