V4n1ll4
V4n1ll4

Reputation: 6099

Create new row in database from form array using 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]">

Upvotes: 0

Views: 481

Answers (1)

Pᴇʜ
Pᴇʜ

Reputation: 57683

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);
}

Upvotes: 1

Related Questions