普通网友 2015-09-24 00:28
浏览 65
已采纳

laravel如何插入多个值

hello everyone i'm beginner in laravel, i want to know how do i inserting a multiple value to a table like, i have an order_detail table on my database and it has order_id, product_id, price and total,

i want to insert a multiple value so i create the form like this which can be added using javascript

<?php for($x = 1;$x <=2; $x++){ ?>

  {{ Form::text('', $x]) }}
  {{ Form::text('pj[$x]order_id') }}
  {{ Form::text('pj[$x]product_id) }}
  {{ Form::text('pj[$x]price) }}
  {{ Form::text('pj[$x]total) }}
  {{ Form::submit('insert!') }}

<?php } ?>

and then i try something like this in my OrderDetailController

$inputs = Input::get('pj');

if(DB::table('order_detail')->insert($inputs)){
  return Redirect::route('admin.order_detail.index')
                  ->with('message','success');
}
return Redirect::back()
         ->with('message','something went wrong')
         ->withInput();

but i only get 1 value from my input which is the last one

ps: this is my first time asking question in this forum, so if you need any information feel free to ask me, and pardon for my bad English, thanks in advance!

  • 写回答

3条回答 默认 最新

  • douqiao6563 2015-09-25 01:33
    关注

    thanks!! really thanks for both of you. here, i combine my code with the reference from both of your code

    my form

    @for($x = 1; $x <=2; $x++)
    
      {{ Form::text('', $x]) }}
      {{ Form::text('pj['. $x .'][order_id]') }}
      {{ Form::text('pj['. $x .'][product_id]') }}
      {{ Form::text('pj['. $x .'][price]') }}
      {{ Form::text('pj['. $x .'][total]') }}
    
    @endfor
    
    {{ Form::submit('insert!') }}
    

    and here's my function

    $pj = Input::get('pj');
    
    foreach($pj as $order) {
      if(DB::table('order_detail')->insert($order)) {}
      else {
        return Redirect::route('admin.order_detail.index')->with('message','failed');
      }
    }
    
    return Redirect::route('admin.order_detail.index')->with('message','success');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?