我來這裏請求一些關於這種情況的邏輯的幫助,如果可能的話,還有一些與代碼有關的幫助。Laravel多個插入到一個表中
所以,這是事情。假設我有這兩張桌子,當然有一對多的關係:
*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
現在好吧。我想要做的就是創建一個鏈接,它將只用一個表單向表中添加一行。是這樣的...
的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
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>');
});
});
好了,我覺得這是很清楚的。當然,我也會在每一行添加特定的評論ID,但我認爲在這裏沒有必要這樣做。
事情是我不知道該怎麼做PHP明智。在我的控制器中寫什麼,以便它能夠檢測所有行併爲每行中的數據創建數組,然後驗證並插入它。 任何人都可以幫我一個忙嗎?
所以,你有一個職位讀後感到控制器,並且要插入審查,以數據庫形式?你有沒有使用雄辯的ORM設置你的模型?他們的評論是否單獨提交?或有人可以繼續點擊「再寫一個評論」按鈕,然後在一個請求中提交多個評論? – Jeemusu
書籍和評論的東西只是一個例子。我希望能夠多次點擊該鏈接,以便填寫相同模型的多種形式,並通過單擊提交按鈕將它們全部插入到表格中。 – arrigonfr