首页 > 解决方案 > 如何在 LARAVEL_7 中通过迁移发送多个复选框

问题描述

我想将选定的检查存储在一个变量中并将其发送到数据库,我只使用迁移,其他数据在迁移中发送。

Pd:我通过数组得到这个错误:ErrorException 数组到字符串的转换

我的模板。

<form action="{{ route('save') }}" 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>

我的控制器:

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');
   
      

    }

我的模型:

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

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

我的迁移:

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();
    });
}

在此处输入图像描述

如您所见,错误是由于复选框我不知道如何将其作为单个数组发送并将其存储在数据库中,我将不胜感激。我正在调查,我没有发现任何东西

标签: htmllaravelcheckboxphpmyadminlaravel-blade

解决方案


    $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'
    ]);

   // Add this line
   $area = [];
   foreach ($request->Areas as $key => $value) {
      $area[$key] = $value;
   }
   // And then save in database with $area created using foreach. 

推荐阅读