普通网友 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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器