jeudi 8 septembre 2022

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="" method="post">
        @csrf
        <div class="row col-12">
                      <div>
                        <p class='mybox text-dark'><input type="checkbox" name="prod_name[]" value=""/>
                    <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.




Aucun commentaire:

Enregistrer un commentaire