为了账号安全,请及时绑定邮箱和手机立即绑定

传递给 Illuminate\Database\Grammar::parameterize()

传递给 Illuminate\Database\Grammar::parameterize()

PHP
慕村225694 2023-07-01 13:10:07
很好,我有一个原始属性,正在创建一个属性,该属性具有用户通过复选框选择的特征,我想知道如何使用属性 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');        });    }
查看完整描述

2 回答

?
慕丝7291255

TA贡献1859条经验 获得超6个赞

看起来你的$request->input('characteristic');是一个数组而不是字符串。


你需要一个循环来解决这个问题。一种方法可以是:


$data = []


foreach ($request->input('characteristic') as $characteristic) {

   $data[] = [

      'characteristic' => $characteristic,

      'property_id' => '' // something

   ];

}


Characteristic::insert($data);

->saveMany()更好的方法是通过设置关系来使用hasMany()。


$characteristics = [];


foreach ($request->input('characteristic') as $characteristic) {

   $characteristics = new Characteristic([

        'characteristic' => $characteristic

   ]);

}


$property->characteristics()->saveMany($characteristics);


查看完整回答
反对 回复 2023-07-01
?
千万里不及你

TA贡献1784条经验 获得超9个赞

我能够保存数据如下


$piso_id = $properti->id;

        

            foreach ($request->input('characteristic') as $characteristic) {

               

               $charc = new Characteristic;

               

               $charc->property_id = $piso_id;

               $charc->characteristic = $characteristic;

               $charc->save();

            }


查看完整回答
反对 回复 2023-07-01
  • 2 回答
  • 0 关注
  • 91 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信