I need to understand how to use the laravel eloquent way to update multiple rows in a database, based on which checkboxes have been checked. I am using the following code right now, which is working to a certain extent:
public function save()
{
$post_data = Input::get('checkbox');
if(is_array($post_data))
{
foreach ($post_data as $checkbox)
{
$product = Products::find($is_checked);
if(!empty($checkbox))
{
$product->is_checked = '1';
}
else {
$product->is_checked = '0';
}
$product->save();
}
}
Session::flash('success', 'Success message here');
return Redirect::to('overview');
}
It does save the "is_checked" values, when a checkbox is selected. In the database it sets the value to "1". But if I uncheck a checkbox, it does not update the row and the "is_checked" stays at "1".
But that is not my main concern. What I wish to do, is not to use a foreach for this, because I need to update multiple rows each time, usually I need to update 100+ rows, so running 100 seperate queries is not the way to go.
First question: How do I update multiple rows in my mysql table by checking or unchecking checkboxes using the laravel eloquent model?
Second question How do I update other fields with different values based on which checkbox has been selected and based on the dropdown that comes with it? Example below:
<form name="bla">
<input type="checkbox" name="checkbox[]" value="{{ $id from database }}>
<select name="number">
<option value="1">1</option>
<option value="2">2</option>
And so on..
</select>
</form>
I have multiple checkboxes with a select menu behind it. What I wish to accomplish is the following:
[x] Checkbox with ID 1 - Select menu option selected = 1
[x] Checkbox with ID 2 - Select menu option selected = 5
[x] Checkbox with ID 3 - Select menu option selected = 10
When I select three checkboxes, all 3 corresponding rows should be updated in the database. So in the above example, the following should happen:
Database row with id 1 should get a "1" value for "is_checked" and the "number" field should be "1" Database row with id 2 should get a "1" value for "is_checked" and the "number" field should be "5" Database row with id 3 should get a "1" value for "is_checked" and the "number" field should be "10"
I hope this explanation makes it clear as to what I want to accomplish and I hope it is not confusing in any sort of way.
Aucun commentaire:
Enregistrer un commentaire