Save multiple checkbox - Laravel 5.4 - php

I need to save multiple checkbox to single field in the database.
<div class="checkbox">
<label>
<input type="checkbox" name="expresion_vegetal_id[]" value="1">Raíz
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="expresion_vegetal_id[]" value="3">tronco
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="expresion_vegetal_id[]" value="4">corteza
</label>
</div>
Controller:
$ficha_tecnica = new Ficha_Tecnica();
$options = $request->get('expresion_vegetal_id');
$ficha_tecnica->expresion_vegetal_id = $options;
$ficha_tecnica->save();
this is trying to save, the values ​​in [""], I need only save the numbers
insert into `fichas_tecnicas` (`expresion_vegetal_id`) values (["1","3","4"])
When I try to save, show the next message
1366 Incorrect integer value: '["1","4"]' for column 'expresion_vegetal_id'

You'r cannot add that because
You can try to loop the expresion_vegetal_id still in array format.
I see in your question is you need to add that in string format.
You must loop that array first and save one by one or you can used created
I give you the sample using loop.
You code will looks like this
$ficha_tecnica = new Ficha_Tecnica();
foreach ($$request->expresion_vegetal_id as $expresion_vegetal_id) {
$ficha_tecnica->fresh();
$ficha_tecnica->expresion_vegetal_id = $expresion_vegetal_id;
$ficha_tecnica->save();
}
i hope you can find the other same way....

Related

How to insert two different values together into database whereas checkbox value and another input value such as quantity

I'm trying to insert two different value one is from a checkbox and one from different value such as quantity of the checkbox value beside it. I'm using foreach below as I am retrieving the books name saved into the database into the checkbox.
#foreach($data as $data)
<form action="{{url('/bookreservation')}}" method="post">
#csrf
<div class="row col-12">
<div>
<p class='mybox text-dark'><input type="checkbox" name="prod_name[]" value="{{$data->title}}"/>{{$data->title}}
<input type="number" name="prod_qty[]" min="1" value="1"class="form-control">
</div>
#endforeach
</div>
</form>
When I use the code below it successfully inserts the data into my database, however it only saves the prod_name since that is the only thing I implode and inserted to my database.
public function bookreservation(Request $request)
{
$data = new bookreservation;
$data->name=$request->name;
$data->email=$request->email;
$data->phone=$request->phone;
$data->address=$request->address;
$data->date=$request->date;
$data->time=$request->time;
$prod_qty = $request->prod_qty;
$selected_products = $request->prod_name;
$products = implode(" ",$selected_products);
$data->products=$products;
$data->save();
return redirect()->back();
}
When I try to implode it together it gives me an error that "implode() expects at most 2 arguments, 3 given"
$prod_qty = $request->prod_qty;
$selected_products = $request->prod_name;
$products = implode($prod_qty," ",$selected_products);
$data->products=$products;
and if I remove " " inside the implode a different error is given "implode(): Argument #1 ($separator) must be of type string, array given" because I want to save both as one value and be inserted into the database as one as well and not separately.
What should I do to save two different values into one. Where prod_qty is beside or before the selected_products like 32 JasminBooks and if they insert/select another different value like 5 KnowHowBooks the result will be "32 JasminBooks, 5 KnowHowBooks," in the database and both are not separated.
First of all, there seems mistake in html structure, you are using forEach loop, which has form inside it and it's end statement is before "form" end tag.
You may follow html structure something like this:
<form action="{{url('/bookreservation')}}" method="post">
<?php foreach ($content as $row):?>
<tr>
<td><input type="text" class="form-control" value="<?php echo $row['title']?>" name="title[]"></td>
<td><input type="text" class="form-control" value="1" name="qty[]"></td>
</tr>
<?php endforeach;?>
<button type="submit" name="submit" value="Submit">Submit</button>
</form>
Then on API controller you may do something like this to iterate over collection and insert record one by one into database:
for($i=0; $i<count($_POST[title]); $i++) {
if(isset($_POST[$i][title])) {
//here goes insert statement
}
}

How to mark as checked the checkbox if the ids is match or exist

I have module where I need checked the list checkbox if the ids determine that i have same ids to the another table. I have two table, Table 1 and Table 2
Table 1 - List of Items and all items loop in the checkbox
Table 2 - This table 2 are the stored ids where the user check the
items on table and submit. and those ids where inserted to the table
2.
So now. If the user try to open again the checkbox list items and if one of those items is already set make the checkbox automatically checked.
I have here my loop items function:
#foreach($details_items as $details_items_data)
<div class="col-md-3">
<div class="form-check">
<input type="checkbox" value="{{$details_items_data->adi_id}}" class="form-check-input csdtc_chkbox" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">{{$details_items_data->items}}</label>
</div>
</div>
#endforeach
Now I have here another loop where this loop is table 2
#foreach($details_set_check as $details_checker)
#endforeach
Currently have the output of this.
Try this out:
#foreach($details_items as $details_items_data)
<div class="col-md-3">
<div class="form-check">
<input type="checkbox" value="{{$details_items_data->adi_id}}" class="form-check-input csdtc_chkbox" id="exampleCheck1" #foreach($details_set_check as $details_checker) {{($details_items_data->adi_id == $details_checker->id)? 'checked':''}} #endforeach>
<label class="form-check-label" for="exampleCheck1">{{$details_items_data->items}}</label>
</div>
</div>
#endforeach
You have to make if condition.
<input #if(value in the lopp exists in your table) checked #endif />

how to receive an incremented id though for each in controller laravel 5

i have a form that i increase the id of checkbox through a foreach with unknown size now when i submit i want to receive all of the sent data here is my code :
<form action="/somecontroller" method="post">
<div id="checkboxes" class="col-lg-2 text-center">
<input type="checkbox" name="rGroup" name="d{{$index}}" value="{{verta($pdates->date)->format('Y/m/d')}}" id="d{{$index}}"/>
<label class="whatever mt-3" for="d{{$index}}"> {{verta($pdates->date)->format('Y/m/d')}}
<hr>
{{$pdates->price}}</label>
</div>
</form>
and here is the controller :
$recived_data = $request->d{{$index}}; here i want to get all the checkboxes send by user
so how can i recive the data that user send which i dont know the number of checkboxes
You can use array syntax of the input element, and receive the array with selected items in the controller, so change your input checkbox to this:
<input type="checkbox" name="d[{{$index}}]" value="{{verta($pdates->date)->format('Y/m/d')}}" id="d{{$index}}"/>
Then in your controller:
$request->input('d'); // returns an array of indexes of all the selected checkboxes.

Laravel set checkboxes checked based on database value

I am trying to set checkboxes checked based on database value. One user can have have multiple checkbox values.
Checkboxes are generated from database table called child_age_groups:
#foreach ($child_age_groups as $age)
<div class="checkbox checkbox-info checkbox-inline">
<input class="age_group_checkbox" type="checkbox" name="age_group[]" id="age_group{{$age->id}}" "/>
<label for="age_group{{$age->id}}">{{$age->age_group}}</label>
</div>
#endforeach
So in my controller I get all the options that user has like this
$nanny_babysitting_ages = Nanny_babysitting_ages::where('user_id', user()->id)->get();
My nanny_babysitting_ages table is this
user_id | ages
and my child_age_groups table where I populate the checkboxes are this:
id | age_group
How can I set the checkboxes selcted based on the values from database?
This is how I resolved it, I added foreach and if statement in the input to check if it the same value as from database:
<div class="form-group" id="ageCheckboxes">
#foreach ($child_age_groups as $age)
<div class="checkbox checkbox-info checkbox-inline">
<input class="age_group_checkbox" type="checkbox" value="{{$age->id}}" name="age_group[]" id="age_group{{$age->id}}" #foreach ($nanny_babysitting_ages as $ages) #if($age->id == $ages->ages ) checked #endif #endforeach />
<label for="age_group{{$age->id}}">{{$age->age_group}}</label>
</div>
#endforeach
</div>
you can use this "#checked(true)" like :-
<input class="custom-control-input army" type="checkbox" id="customCheckbox1" #checked($emp_plus->army != 0)>

PHP: POST paired check box/text field in form

I have a form that is a list of check boxes. Next to each check box is a text field that goes with the check box. Below is an image of what I am talking about
The text in red is the name attribute. My question is based off of whatever the user check marks, how do I match the paired text field without doing a bunch of if statements?
I know how to just post the check boxes and just post the text fields..separately. I guess I am stuck at how to pair the results. This is what I am using to $_POST the check boxes
if (isset($_POST['family_medical_history']))
{
$_SESSION['patient_form']['family_medical_history'] = array();
foreach ($_POST['family_medical_history'] as $key => $value)
{
$_SESSION['patient_form']['family_medical_history'][$key] = $_POST['family_medical_history'][$key];
}
}
else
{
$_SESSION['patient_form']['family_medical_history'] = "none";
}
Example HTML form
<div class="form-group col-sm-4">
<label><input type="checkbox" name="family_medical_history[]" value="Crossed Eyes" /> Crossed Eyes</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes">
</div>
You can pair them by naming the inputs similarly for each checkbox/relationship pair to produce a two-member array for each item.
<div class="form-group col-sm-4">
<label><input type="checkbox" name="rel_crossed_eyes[]" value="Crossed Eyes" /> Crossed Eyes</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes[]">
</div>
In the PHP, they will become pairs with the 0 element equal to the value of the checkbox input, and the 1 element equal to the text input. You can create an array of conditions and loop through each of the named inputs. You'll have to change your if statement to something else, for example the submit button:
$conditions = array("rel_blindness" => "Blindness", "rel_cataracts" => "Cataracts", "rel_crossed_eyes" => "Crossed Eyes", "rel_glaucoma" => "Glaucoma");
if (isset($_POST['submit'])){
foreach ($conditions as $key => $value){
if ($_POST[$key][0] == $value){//if the checkbox is checked
$_SESSION['patient_form']['family_medical_history'][$key] = $_POST[$key][1];
}
}
}

Categories