Sunday, July 5, 2020

Add more & Remove in Laravel ?

blade.php :

<div class="row" style="margin-top: 36px">
                                <div class="col-lg-12">
                                    <div class="box-body no-padding">
                                        <div class="table-responsive">
                                            <table align='center' class="table table-responsive" spacing='2' cellpadding='5' style="width: 700px;">
                                                <thead>
                                                <tr style="background: lightgrey">
                                                    <th>Product Name</th>
                                                    <th>Action</th>
                                                </tr>
                                                </thead>
                                                <tbody id="productAppendDiv">
                                                        <tr class="row_element">
                                                            <td style="width:700px;"> 
                                                              <input  type="text" name="product_name[]" class="form-control multiple_product" placeholder="Type product name"/>
                                                             
                                                            </td>
                                                            <td>
                                                                <button type="button" class="btn btn-danger deleteProductRow " style="width: 40px; height: 40px"><i class="fa fa-trash"> </i></button>
                                                            </td>
                                                        </tr>
                                                </tbody>
                                            </table>
                                            <div class="col-md-8">
                                                <button type="button" class="btn btn-success btn-sm add pull-right" id="addMoreProducts"><span class="fa fa-plus"></span> Add More Product</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                            </div>


<table id="productHideDiv" style="display: none">
    <tr class="row_element"> 
        <td style="width: 700px;">  
          <input  type="text" name="product_name[]" class="form-control multiple_product" placeholder="Type product name"/>

        </td>
        <td>
            <button type="button" class="btn btn-danger deleteProductRow " style="width: 40px; height: 40px"><i class="fa fa-trash"> </i></button>
        </td>
    </tr>
</table>


<script type="text/javascript">
  $( document ).ready(function() {
        $('#addMoreProducts').click(function(){
            var cloned=$('#productHideDiv tr').first().clone(true);
            cloned.appendTo('#productAppendDiv');
           
        });
    });

    $(document).on("click", ".deleteProductRow", function () {
        $(this).parents('.row_element').remove();
    });
</script>

controller :

 public function store(Request $request)

    {
      $post = new BlogPost;
       $post->save();

   
      $productDetails  = $this->makeProductDataFormat($request->product_name,$post->id);

          if(count($productDetails) > 0) {
              BlogProduct::insert($productDetails);
          }
}


public function makeProductDataFormat($data,$postId) {

       BlogProduct::where('post_id',$postId)->delete(); //use  update function
       
        $productDetailsData = [];
        if(isset($data)){
          foreach ($data as  $value) { 
               $product = Product::where(['status'=>1, 'name'=>$value])->first();
               if($product){
                 $productDetailsData[] =[
                    'post_id'     => $postId,
                    'product_id'  => $product->id,
                ];
               } 
            }
        }
       
        return $productDetailsData;

    }

No comments:

Post a Comment

Ajax load lage with laravel.

 step-1:  HTML <div class="row">                     <div class="col-lg-12">                           <d...