dongyange1101 2019-02-14 13:28
浏览 52
已采纳

Laravel 5.4:从一个表单中插入每个项目作为行

I have a form which insert multiple products here it's my form

{!! Form::open(['route'=>'export-list.store']) !!}
        @foreach($orderis as $orderi)
           {!! Form::hidden('product_id[]', $orderi->productId->id) !!}
           {!! Form::hidden('quantity[]', $orderi->quantity) !!}
               <tr>
                  <td>{!! $sn++ !!}</td>
                  <td width="5%">
                  {!! Html::image('images/products/'. $orderi->productId->image, 'thumbs-'.$orderi->productId->name, ['height' => 70]) !!}
                  </td>
                  <td>{!! $orderi->product_name !!}</td>
                  <td>{!! $orderi->quantity !!}</td>
                  <td>
                     <div class="col-md-5">
                        <div class="form-group-inner">
                       {!! Form::text('inventory[]', null, ['class'=>'form-control']) !!}
                        </div>
                     </div>
                 </td>
                 <td>{!! $orderi->productId->price !!}</td>
                 <td>{!! $orderi->productId->price * $orderi->quantity !!}</td>
               </tr>
           @endforeach
               <tr>
                 <td colspan="9">{!! Form::submit('Submit', ['class'=>'btn btn-primary']) !!}</td>
               </tr>
{!! Form::close() !!}

The end results is an array.

array:4 [▼
  "_token" => "cpua1RzKKP4vvklB99HafAdMQ65TSxOWQVQ5r3ye"
  "product_id" => array:3 [▼
    0 => "856"
    1 => "857"
    2 => "858"
  ]
  "quantity" => array:3 [▼
    0 => "9"
    1 => "8"
    2 => "2"
  ]
  "inventory" => array:3 [▼
    0 => "8"
    1 => "2"
    2 => "6"
  ]
]

and here is my controller

public function store( Request $request ) {
        $input = $request->all();

        //Check for the needed quantity
        $quantity        = $input['quantity'];
        $inventory       = $input['inventory'];
        $needed_quantity = $quantity - $inventory;
        //Get the product information
        $productInfo = ( new Product() )->find( $input['product_id'] );
        $totalPrice  = $productInfo->price * $needed_quantity;


        ( new PurchaseOrder() )->create( [
            'product_id'      => $input['product_id'],
            'product_name'    => $productInfo->translate( 'ar' )->name,
            'product_image'   => $productInfo->image,
            'needed_quantity' => $needed_quantity,
            'unit_price'      => $productInfo->price,
            'total_price'     => $totalPrice,
            'barcode'         => $productInfo->barcode,
        ] );

        return redirect()->route( 'order-detail.index' )->with( 'status', 'Created successfully' );
    }

How to insert each item as row in my table

  • 写回答

1条回答 默认 最新

  • dongzhang1839 2019-02-17 12:36
    关注

    Many thanks to @SNAPEY who helped me to fix my issue

    it was more easier when I named my fields differently... as

    @foreach($orderis as $orderi)
            {!! Form::hidden('products[' . $loop->index . '][product_id]', $orderi->productId->id) !!}
            {!! Form::hidden('products[' . $loop->index . '][quantity]', $orderi->quantity) !!}
            {!! Form::text('products[' . $loop->index . '][inventory]', null, ['class'=>'form-control']) !!}           
    @endforeach
    

    so the end results was something like this

    array:2 [▼
      "_token" => "muw3phvtnjnJnjBYNqbp0A5f6Pk5St1BkydKPGG7"
      "products" => array:3 [▼
        0 => array:3 [▼
          "product_id" => "856"
          "quantity" => "9"
          "inventory" => "2"
        ]
        1 => array:3 [▼
          "product_id" => "857"
          "quantity" => "8"
          "inventory" => "3"
        ]
        2 => array:3 [▼
          "product_id" => "858"
          "quantity" => "2"
          "inventory" => "4"
        ]
      ]
    ]
    

    and it was very easy to manage the data in my controller with foreach like this

    $eachProducts = $input['products'];
    
    foreach ( $eachProducts as $each_product ) {
        $quantity        = $each_product['quantity'];
        $inventory       = $each_product['inventory'];
        ......
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀