微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 LARAEL_7 中通过迁移发送多个复选框

如何解决如何在 LARAEL_7 中通过迁移发送多个复选框

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

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

我的模板。

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

enter image description here

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

解决方法

    $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. 
,

使用这个'Areas.*'=> 'required',一般用来校验checkbox,另外在Areas下插入数据,可以通过json_encode($request->Areas);转换插入

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。