doufendi9063 2015-06-29 10:18
浏览 55
已采纳

使用Eloquent / Laravel-5从表单数组创建数据库中的新行

I've created a simple function which creates a new row in my database from a form post. However, I have changed the HTML form so that you are able to add multiple rows and now of course the form is posted as an array to the function and therefore the Laravel fill() function will no longer work.

How can I adapt my code to allow for an array?

Here is my controller method:

/**
 * Create new resource.
 *
 * @return Response
 */
public function create(Location $location, LocationRequest $request, Input $input, Response $response)
{
    $location->fill($input->all());

    return $response->json(array('success' => true, 'msg' => 'New location created.'));
}

My form looks like this:

<input class="inp" type="text" name="location[0][location_name]">
<input class="inp" type="text" name="location[0][address]">
  • 写回答

1条回答 默认 最新

  • dongxiz5342 2015-06-29 11:42
    关注

    To give you a hint: Try to iterate through your array and run fill() for each row you want to add. This could look something like this:

    foreach ($input->location) as $loc) {
        $location->fill($loc);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化