如何在Laravel数据库的一列中插入多个字段数据

时间:2019-02-21 10:13:00

标签: laravel-5.7

我想使用此字段,我想在此description []中插入description1,description2数据[]我尝试过的操作,但是缺少一些东西。

<div class="form-group row">
    <label for="description" class="col-md-4 col-form-label">Description</label>
    <div class="col-md-8">
      <input type="text" class="form-control" name="description[]" placeholder="Enter description">
    </div>
  </div>

  <div class="form-group row">
    <label for="description" class="col-md-4 col-form-label">Description 1 (optional)</label>
    <div class="col-md-8">
      <input type="text" class="form-control" name="description2" placeholder="Enter description">
    </div>
  </div>

<div class="form-group row">
    <label for="description" class="col-md-4 col-form-label">Description 2 (optional)</label>
    <div class="col-md-8">
      <input type="text" class="form-control" name="description3" placeholder="Enter description">
    </div>
  </div>



     public function store(Request $request)
        {
$description1 = $request->input('description1');
        $description2 = $request->input('description2');
        $products = new ProductAdd;
            $prodduct->name = $request('name');
            $prodduct->description[] = [
                'description1' => $description1;
                'description2' => $description2;
        ];
            $product->save();
}

1 个答案:

答案 0 :(得分:0)

如果将description字段强制转换为模型中的数组,则可以将控制器代码更新为:

$prodduct->description = [
    'description1' => $description1;
    'description2' => $description2;
];

要将字段转换为数组,请将其添加到您的ProductAdd模型中:

$casts = [
    'description' => 'array'
];