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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?