3

I come here requesting some help with the logic of this case, and if possible, some help with the code as well.

So here's the thing. Let's assume I have these two tables, which of course, have a One-Many relationship:

*These aren't the real tables, they're just to simplify things*

**Table books**
id
name

**Table books_reviews**
id
books_id
user_id   <-this would be the person who wrote a review
details   <-A varchar 140 field
rating    <-A 1 through 10 integer field

Ok now. What I want to do is create a link, that will just append one more row to a table with the whole form. Like so...

The HTML

<a href="#" id="myLink">Write one more review</a>
<table id="mytable">

</table>
<input type="submit">   <- This should sumbit all the rows for validation and insertion

The Javascript

$(document).ready(function(){
    var click=0;
    $('#myLink').click(function(){
        click++;
    $('#mytable').append('<tr>
          <td><input type="text" class="form-control" id="details'+click+'" name="details'+click+'"</td>
          <td><input type="text" class="form-control" id="rating'+click+'" name="rating'+click+'"</td>
                         </tr>');
    });
});

Ok, so I think this is pretty clear. Of course I would also append the specific reviews id to each row, but i didn't think it would be necessary to do that here.

The thing is I cant figure out what to do PHP-wise. What to write in my controller so that it will detect all the rows and create the arrays for the data in each row, then validate and insert it. Could anyone give me a hand with this?

arrigonfr
  • 742
  • 3
  • 12
  • 34
  • So you have a form that posts a book review to a controller, and you want to insert that review to your database? Have you set up your models using Eloquent ORM? Are they reviews submitted individually? or can someone keep clicking the "write one more review" button and then submit multiple reviews in one request? – Jeemusu May 23 '14 at 02:21
  • The books and reviews thing is just an example. What I want to be able to click that link more than once, so I can fill in, many forms of the same model, and inserting them all into the table by clicking on the submit button. – arrigonfr May 23 '14 at 03:01

1 Answers1

4

If you look at the source code generated by your javascript, you should see that the names of your inputs would be like:

details1 rating1
details2 rating2
...

This is probably not the best option, I recommend you name all your inputs like details[]and rating[]. There's no need to use the counter.

Like you probably know, in laravel you should use Input::all() to get all the form data. This function should return to you the following array:

# array: form data
array(
    'details' => array(
        [0] => 'Content of details 1',
        [2] => 'Content of details 2'
    ),
    'rating' => array(
        [0] => 'Content of rating 1',
        [2] => 'Content of rating 2'
    )
)

To insert multiple rows at once with laravel you can use the function BookReview::insert($array), this function receives an array of arrays to be added in the database. This array should be like this one:

# array: eloquent ready
array(
    array(
        'details' => 'Content of details 1',
        'rating' => 'Content of rating 1',
    ),
    array(
        'details' => 'Content of details 2',
        'rating' => 'Content of rating 2',
    ),
)

So, all you have to do is convert the array 'form data' to the array 'eloquent ready'. This can be done with a simple algorithm:

$input = Input::all();
$insert = array();
foreach($input['details'] as $key => $detail) {
    $insert[$key]['details'] = $detail;
}
foreach($input['rating'] as $key => $rating) {
    $insert[$key]['rating'] = $rating;
}
BookReview::insert($insert);

PS: In my examples I did not add the other fields, like user_id and book_id. You should add it on the foreach to add this info into all rows.

Lucas Ferreira
  • 858
  • 8
  • 18
  • Awesome! That's what I need exactly. Haven't tried it yet, but I just know it will work just by reading your code. Just one more thing: What would be the best way to validate every row so using ajax so the url won't reload, and just tell me where the errors are? – arrigonfr May 23 '14 at 03:12
  • 1
    You may validate it on the client side or the server side. On the client side you may use the `required` parameter on the input or use javascript (I recommend the Parsley library). On the server side a lot more complex, because you have to validate each model. To do this in the server side I would recommend instead of making an array of arrays, make an array of BookReview objects, so you could go through each one of them and validate them one by one using the laravel's `Validation` class. This would be a lot of work, since you would have to send the errors back to javascript and handle them. – Lucas Ferreira May 23 '14 at 03:25