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

很好,我有一个原始属性,正在创建一个属性,该属性具有用户通过复选框选择的特征,我想知道如何使用属性 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');


        });

    }



慕村225694
浏览 118回答 2
2回答

慕丝7291255

看起来你的$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);

千万里不及你

我能够保存数据如下$piso_id = $properti->id;                    foreach ($request->input('characteristic') as $characteristic) {                              $charc = new Characteristic;                              $charc->property_id = $piso_id;               $charc->characteristic = $characteristic;               $charc->save();            }
打开App,查看更多内容
随时随地看视频慕课网APP