Kanchan186's avatar

how to save multiple checkbox values in database in laravel

prodview.php

<body>
        <div class="flex-center position-ref full-height">
            

            <div class="content">
               
                <form method="post" action="{{url('/')}}/store1" enctype="multipart/form-data">
                        {{csrf_field()}}
                <input type="hidden" name="MAX_FILE_SIZE" value="10485760">


                     <table  class="table table-striped">
                        
                   
                        <tr> <td><select name="p_id" class="form-control">
                             @foreach($product as $prod)
                             <option value ="{{$prod->p_id}}">{{$prod->p_name}}</option>
                             @endforeach
                             </select></td> 
                        </tr>

                        <tr><td>
                            @foreach($category as $cat)
                            <input type="checkbox" name="c_id"  value ="{{$cat->c_id}}">{{$cat->c_name}}<br>
                            @endforeach</td>

                        <tr><td><input type="submit" name=""></td></tr>


                       </table> </form><br><hr>
               
            </div>
        </body>

prodCatController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\prod_cat;
use App\product;
use App\category;



class ProdCatController extends ProductController
{
   public function store1(Request $req)
    {
        //dd($req->all());
        prod_cat::create([
                    'p_id'=>request('p_id'),
                    'c_id'=>request('c_id'),
                   ]);
        
         $product=product::get();
         $category=category::get();
         $prod_cat=prod_cat::get();

        return redirect('pcview');
    }



    public function show1()
                 {

                    

                    $product=product::get();
                    $category=category::get();
                     $prod_cat=prod_cat::get();


                    return view('pcview',compact('product','category','prod_cat'));

                    //return redirect('prodview');
                  } 



}
0 likes
2 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Use name="c_id[]" to store the checkmarks in an array when sent to the controller. If they need to be in one field/row in the database, you could json encode the checkboxes and decode them when retrieved. Alternatively you can have a row for each product/category relation

Kanchan186's avatar

sir can you pleasee send the short code or sample code to understand the concept

Please or to participate in this conversation.