Laravel 从数组中在数据透视 table 中插入多条记录

Laravel insert multiple records in pivot table from arrays

我试图在 table 中保存多条记录(行)。 html字段是动态字段,声明为数组,所以可以是1个、2个或更多。

我的blade:

<div class="col-md-12" id="inputFormRow" style="padding-left: 0px;">
<div class="input-group mb-3">
    <input type="text" name="tableName[]" class="form-control m-input" placeholder="Name"  autocomplete="off">
    <input type="text" name="fromNr[]" class="form-control m-input" placeholder="From"  autocomplete="off">
    <input type="text" name="toNr[]" class="form-control m-input" placeholder="to" autocomplete="off">
    <div class="input-group-append">                
        <button id="removeRow" type="button" class="btn btn-danger">X</button>
    </div>
</div>
+

我的JS创建动态字段:

$("#addRow").click(function () {
    var html = '';
    html += '<div class="col-md-12" id="inputFormRow"  style="padding-left: 0px;">';
    html += '<div class="input-group mb-3">';
    html += '<input type="text" name="tableName[]"  class="form-control m-input" placeholder="Name" autocomplete="off">';
    html += '<input type="text" name="fromNr[]" class="form-control m-input" laceholder="From" autocomplete="off">';
     html += '<input type="text" name="toNr[]" class="form-control m-input" placeholder="To" autocomplete="off">';
    html += '<div class="input-group-append">';
    html += '<button id="removeRow" type="button" class="btn btn-danger">X</button>';
    html += '</div>';
    html += '</div>';

    $('#newRow').append(html);
});

我的Offer.php模特:

    protected $fillable = ['some columns];
    public function table()
    {
        return $this->hasMany(Table::class);
    }

我的Table.php模特:

protected $fillable = ['offer_id','tableName','fromNr','toNr'];

public function offer()
{
    return $this->hasMany(Offer::class);
}

现在,在我的控制器中,我必须获取请求输入值,然后保存到 Table table。输入值可以大于 1 并且是动态的。

我的尝试:

public function store(Request $request)
{   
    $statement = DB::select("SHOW TABLE STATUS LIKE 'offer'");
    $nextId = $statement[0]->Auto_increment;

    $tableName = $request->get('tableName');
    $fromNr = $request->get('fromNr');
    $toNr = $request->get('toNr');

    $offer = Offer::find($nextId);

    $offer->table()->saveMany([
        new Table(['restaurant_offer_id' => $nextId]),
        new Table(['tableName' => $tableName]),
        new Table(['fromNr' => $fromNr]),
        new Table(['toNr' => $toNr]),
        
    ]);
}

提前致谢。

如果您在视图上使用 name="example[]",您将在控制器中接收作为数组的变量。此外,如果您使用 Eloquent 模型绑定,您可以使用更简单的语法将模型实例保存到数据库中。

在控制器中尝试这样的操作:

public function store(Request $request)
{   
    foreach($request->tableName as $key => $tableName)
    {
        Offer::create(['tableName' => $tableName',
                      'fromNr' => $request->fromNr[$key],
                      'toNr' => $request->toNr[$key]])
    }
   
}

此外,我建议在数组的情况下使用复数命名。像 tableNames,来自 Nrs。所以你知道它应该包含多个变量。

如果你想让它成为动态的,你必须遍历输入数组。

$tables = [];
foreach($tableName as $key => $value) {
    $table = new Table;
    $table->tableName = $tableName[$key];
    $table->fromNr = $fromNr[$key];
    $table->toNr = $toNr[$key];

    $tables[] = $table;
}

$offer->table()->saveMany($tables);