php - How to insert multiple rows into a table at one time?

I need to send e.g. 5 records in one query into the one table (id_book - is every time different), so I need to by one click on button, create e.g. 10 records. This code shows this Error message:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id_book.0' in 'where clause' (SQL: select count() as aggregate fromconnect_user_books
whereid_book
.0
= 6* (MY TEXT: 6 IS ID OF MY LIST INTO WHICH I NEED TO ADD THEASE RECORDS))
My controller:
public function create()
{
$books = Book::all();
return view('connectUserBook.create', compact('books'));
}
public function store(Request $request)
{
$this->validate($request, [
'id_user_select' => 'required',
'id_book' => 'required|array',
'id_book.*' => 'exists:connect_user_books',
]);
$userId = $request->input('id_user_select');
// Vytvoření
foreach ($request->input('id_book') as $book) {
$connect = new ConnectUserBook;
$connect->id_user_select = $userId;
$connect->id_book = $book;
$connect->save();
}
return redirect('/dashboard')->with('success', 'Výběr editován!');
}
My HTML markup:
{{ Form::open(['action' => 'ConnectUserBookController@store', 'method' => 'POST']) }}
<div>
<label class="form-label">Vybraný výběr *</label>
<div>
<select name="id_user_select" class="form-control">
<?php
$select_id = $_GET["id"];
$select_id;
$res = mysqli_query($link, "select * from selected_books Where id = $select_id");
while($row = mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row["id"]; ?>"> <?php echo $row["title"]; ?></option>
<?php
}
?>
</select>
</div>
</div>
<br>
@for ($i = 0; $i < 11; $i++)
<div>
<label class="form-label">Book {{ $i }} *</label>
<div>
<select name="id_book[]" class="form-control">
@foreach ($books as $book)
<option value="{{ $book->id_book }}">{{ $book->nazev }} - {{ $book->pocet_stranek }} stran</option>
@endforeach
</select>
</div>
</div>
@endfor
{{Form::submit('Submit', ['class' => 'btn btn-primary'])}}
{{ Form::close() }}
Answer
Solution:
Make an array and use the insert method with eloquent.
$data = array()
foreach ($request->input('id_book') as $book) {
$data[] = array('id_user_select'=>$userId,'id_book'=>$book);
}
ConnectUserBook::insert($data);
as per your error your need to specify with the column. add a column to compare with which column exists?
'id_book.*' => 'exists:connect_user_books,column_name',
Note :- It doesn't insert the timestamps.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: use the option --with-all-dependencies (-w) to allow upgrades, downgrades and removals for packages currently locked to specific versions.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.