如何在 LARAVEL_7 中通过迁移发送多个复选框
Posted
技术标签:
【中文标题】如何在 LARAVEL_7 中通过迁移发送多个复选框【英文标题】:How to Send Multiple Checkbox through migration in LARAVEL_7 【发布时间】:2021-05-03 22:21:32 【问题描述】:我想将选定的检查存储在单个变量中并将其发送到数据库,我只使用迁移,其他数据在迁移中发送。
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
如您所见,错误是由于复选框我不知道如何将其作为单个数组发送并将其存储在数据库中,我将不胜感激。我正在调查,我没有发现任何东西
【问题讨论】:
【参考方案1】: $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.
【讨论】:
【参考方案2】:使用'Areas.*'=> 'required'
这个'Areas.*'=> 'required'
,一般用于校验复选框,另外要在Areas下插入数据,可以通过json_encode($request->Areas);
转换插入
【讨论】:
谢谢你这么多兄弟以上是关于如何在 LARAVEL_7 中通过迁移发送多个复选框的主要内容,如果未能解决你的问题,请参考以下文章
如何在 laravel 身份验证期间附加到用户创建多个复选框