【问题标题】:How to Save an array index data to database in separate row?如何在单独的行中将数组索引数据保存到数据库中?
【发布时间】:2020-03-18 01:45:56
【问题描述】:

我有一个属于产品表的特色表(外键 = product_id),现在我想将一些产品 id 保存到我的特色表中,它给了我一个数组结果,但我无法将其保存到数据库中强>

这是我的控制器 -->

  public function featuredProduct(Request $request)
   {
       if($request->isMethod('POST')){
         $product_id=$request->all();
         foreach ($product_id as $product) {
            $products[]=$product;
         }
         //dd($products);
       Featured::create($products);
       }
       return view('admin.products.featured');
   }
<form action="{{ route('featuredProduct') }}" method="POST" multiple>
  <table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap verticle_middle">
    <thead>
      <tr>
        <th>Product</th>
        <th>Category</th>
        <th>Image</th>
        <th>Status</th>
      </tr>
    </thead>
    <tbody>
    @foreach ($products as $product)
      <tr>
        <td>
          <input id="{{ $product->id }}" value="{{ $product->id }}" type="checkbox" name="product[]">
          <label for=id={{ $product->id }}>{{ $product->product_name }}</label>
        </td>
        <td> {{ $product->category['cat_name'] }} </td>
        <td> <img src="{{asset($product->pro_img) }}" alt="" width="40"> </td>
        <td class="center">
        @if ($product->status === 1)
            <span class="btn btn-primary btn-xs">Published</span>
        @else
            <span class="btn btn-warning btn-xs">Unpublish</span>
        @endif
        </td>
        </tr>
          @endforeach
       </tbody>
    </table>
    <div class="modal-footer">
      <button type="button"class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
      <button type="submit" class="btn btn-primary waves-effect waves-light">Submit</button>
    </div>
</form>

【问题讨论】:

    标签: php laravel eloquent laravel-blade


    【解决方案1】:

    您可以在 for 循环中创建每个功能。

    public function featuredProduct(Request $request)
    {
        if($request->isMethod('POST')) {
            foreach($request->all() as $productId) {
                Featured::create([
                    'product_id' => $productId,
                    ... other data fields.
                ]);
            }
        }
    }
    

    或者,如果您想一次保存所有功能。

    public function featuredProduct(Request $request)
    {
        if($request->isMethod('POST')) {
    
            $featureds = [];
    
            foreach($request->all() as $productId) {
                $featureds[] = [
                    'product_id' => $productId,
                    ... other data fields.
                ]
            }
    
            DB::table('featureds')->insert($featureds);
        }
    }
    

    【讨论】:

    • @tharaka_Dilshan 它给我一个空白页?
    • 感谢它为我所做的工作!来自孟加拉国的爱? 我是编程新手,这对我帮助很大。
    猜你喜欢
    • 2017-07-02
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    相关资源
    最近更新 更多