很好,我有一个原始属性,我正在创建一个属性,这个属性有一些特性,用户通过复选框选择,我想知道如何用属性id保存在我的特性表中选择的复选框集
控制器
$characteristics = new Characteristic;
$characteristics->characteristic = $request->input('characteristic');
$characteristics->save();
迁移属性
public function up()
{
Schema::create('properties', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->nullable;
$table->string('price')->nullable;
$table->text('description')->nullable;
$table->unsignedBigInteger('property_type_id')->nullable();
$table->unsignedBigInteger('offer_type_id')->nullable();
$table->unsignedBigInteger('spaces_id')->nullable();
$table->unsignedBigInteger('departaments_id')->nullable();
$table->unsignedBigInteger('municipalities_id')->nullable();
$table->unsignedBigInteger('details_id')->nullable();
$table->unsignedBigInteger('characteristics_id')->nullable();
$table->string('images')->nullable;
$table->float('lat')->nullable;
$table->float('lng')->nullable;
$table->string('address')->nullable;
$table->timestamps();
$table->foreign('property_type_id')->references('id')->on('property_type')->onDelete('cascade');
$table->foreign('offer_type_id')->references('id')->on('offer_type')->onDelete('cascade');
$table->foreign('spaces_id')->references('id')->on('spaces')->onDelete('cascade');
$table->foreign('departaments_id')->references('id')->on('departaments')->onDelete('cascade');
$table->foreign('municipalities_id')->references('id')->on('municipalities')->onDelete('cascade');
$table->foreign('details_id')->references('id')->on('details')->onDelete('cascade');
$table->foreign('characteristics_id')->references('id')->on('characteristics')->onDelete('cascade');
});
}
迁移特征
public function up()
{
Schema::create('characteristics', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('property_id');
$table->string('characteristic');
$table->timestamps();
});
}
与chec合作
<div class="form-group">
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="ascensor" value="ascensor" name="characteristic[]">
<label class="form-check-label" for="ascensor">Ascensor</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="piscina" value="piscina" name="characteristic[]">
<label class="form-check-label" for="piscina">Piscina</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="turco" value="turco" name="characteristic[]">
<label class="form-check-label" for="turco">Turco </label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="jacuzzy" value="jacuzzy" name="characteristic[]">
<label class="form-check-label" for="ascensor">jacuzzy</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="patio" value="patio" name="characteristic[]">
<label class="form-check-label" for="piscina">Patio/Zona Verde</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="ucerrada" value="ucerrada" name="characteristic[]">
<label class="form-check-label" for="turco">Unidad Cerrada </label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="s_basura" value="s_basura" name="characteristic[]">
<label class="form-check-label" for="ascensor">Shut de basura</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="j_infantiles" value="j_infantiles" name="characteristic[]">
<label class="form-check-label" for="piscina">Juegos Infantiles</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="r_transporte" value="r_transporte" name="characteristic[]">
<label class="form-check-label" for="turco">Rutas de transporte </label>
</div>
</div>
2条答案
按热度按时间x4shl7ld1#
好像是你的
$request->input('characteristic');
是数组而不是字符串。你需要一个循环来解决这个问题。一种方法是:
更好的方法是
->saveMany()
通过设置hasMany()
关系。wpx232ag2#
我可以保存数据如下