批量插入口才收藏-Lumen / Laravel

问题描述 投票:0回答:1

我有以下收藏:

[{"Internal_key":"TESTKEY_1","extensiontable_itc":{"description_itc":"EXTENSION_ITC_1"},"extensiontable_sysops":{"description_sysops":"EXTENSION_SYSOPS_1"}},{"Internal_key":"TESTKEY_2","extensiontable_itc":{"description_itc":"EXTENSION_ITC_2"},"extensiontable_sysops":{"description_sysops":"EXTENSION_SYSOPS_2"}},{"Internal_key":"TESTKEY_3","extensiontable_itc":{"description_itc":"EXTENSION_ITC_3"},"extensiontable_sysops":{"description_sysops":"EXTENSION_SYSOPS_3"}},{"Internal_key":"TESTKEY_4","extensiontable_itc":{"description_itc":"EXTENSION_ITC_4"},"extensiontable_sysops":{"description_sysops":"EXTENSION_SYSOPS_4"}},{"Internal_key":"TESTKEY_5","extensiontable_itc":{"description_itc":"EXTENSION_ITC_5"},"extensiontable_sysops":{"description_sysops":"EXTENSION_SYSOPS_5"}}]

它是这样检索的:

$collectionForInsertion = coretable::with($permittedTables)->get();

Coretable及其关联表具有1-1关系。这些关联表的模型基本上都如下所示:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class extensiontable_itc extends Model
{


    /**
   * The table associated with the model.
   *
   * @var string
   */
   protected $table = 'extensiontable_itc';

  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */

   protected $hidden = [
     'id_extensiontable_itc',
     'coretable_id',
     'created_at',
     'updated_at'
   ];


  protected $fillable = [
    'description_itc'
  ];



  /**
   * Many-To-Many relationship with User-Model.
   */
  public function coretable()
  {
    return $this->hasOne('App\coretable', 'coretable_id');
  }
}

他们的迁移看起来像这样:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateExtensiontableItc extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('extensiontable_itc', function (Blueprint $table) {
            $table->bigIncrements('id_extensiontable_itc');
            $table->bigInteger('coretable_id')->unsigned()->unique()->nullable(false);
            $table->foreign('coretable_id', 'fk_extensiontable_itc_coretable')->references('id_coretable')->on('coretable');
            $table->string('description_itc')->nullable(false);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('extensiontable_itc');
    }
}

现在,我想知道它是否确实可行,是否可以使用某些精美的内置雄辩ORM功能进行以下插入:

$collectionForInsertion->push($inputArray);
$collectionForInsertion->save();

当前,这不起作用。save()方法不存在用于收集,至少那是错误消息所提示的:

 (1/1) BadMethodCallException

Method Illuminate\Database\Eloquent\Collection::save does not exist.

此外,使用以下测试输入时:

{
"Internal_key" : "TESTKEY_6",
"description_itc" : "EXTENSION_ITC_6",
"description_sysops" : "EXTENSION_SYSOPS_6"
}

像这样推送到$collectionForInsertion

$collectionForInsertion->push($inputArray);

结果如下:

[{"Internal_key":"TESTKEY_1","extensiontable_itc":{"description_itc":"EXTENSION_ITC_1"},"extensiontable_sysops":{"description_sysops":"EXTENSION_SYSOPS_1"}},{"Internal_key":"TESTKEY_2","extensiontable_itc":{"description_itc":"EXTENSION_ITC_2"},"extensiontable_sysops":{"description_sysops":"EXTENSION_SYSOPS_2"}},{"Internal_key":"TESTKEY_3","extensiontable_itc":{"description_itc":"EXTENSION_ITC_3"},"extensiontable_sysops":{"description_sysops":"EXTENSION_SYSOPS_3"}},{"Internal_key":"TESTKEY_4","extensiontable_itc":{"description_itc":"EXTENSION_ITC_4"},"extensiontable_sysops":{"description_sysops":"EXTENSION_SYSOPS_4"}},{"Internal_key":"TESTKEY_5","extensiontable_itc":{"description_itc":"EXTENSION_ITC_5"},"extensiontable_sysops":{"description_sysops":"EXTENSION_SYSOPS_5"}},{"Internal_key":"TESTKEY_6","description_itc":"EXTENSION_ITC_6","description_sysops":"EXTENSION_SYSOPS_6"}]

数据不以现有数据记录的嵌套方式附加。因此,将这个集合保存到模型/数据库中可能无法正常工作。

但是,有没有一种方法可以使用laravel内置功能轻松完成此任务?还是我需要编写自定义函数,逐个循环访问和访问模型,同时将输入映射到每个模型?

php laravel eloquent orm lumen
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.