Can I ask the question to how to insert the multiple data or information in database using foreach at the controller?
Asked
Active
Viewed 330 times
0
-
by `insert()` function – Kamlesh Paul Sep 08 '20 at 06:20
-
Does this answer your question? [How to insert multiple rows from a single query using eloquent/fluent](https://stackoverflow.com/questions/29723865/how-to-insert-multiple-rows-from-a-single-query-using-eloquent-fluent) – Kamlesh Paul Sep 08 '20 at 06:20
-
It depends on what you wanted to insert. It will be better if you can make it specific. – Ayenew Yihune Sep 08 '20 at 07:10
1 Answers
0
$data = [];
$period = CarbonPeriod::create('2020-09-10', '2020-09-20')->toArray();
foreach ($period as $key => $value) {
$row['register_date'] = $value->format('Y-m-d');
array_push($data, $row);
}
User::insert($data);

Asim Raza
- 80
- 4
-
While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. [From Review](/review/low-quality-posts/27114153) – double-beep Sep 08 '20 at 08:07