jeudi 4 février 2016

session destrioyed on add to cart in laravel

I am using laravel5. I need to add the product into cart. when i add a new product it is add into cart item as 1. but when refresh the page or move to next page my cart item count destroyed. Here i attach my coding. Please help me what is my mistake. i am new to laravel

Controller:

        public function cart($request)
{
    $quick=Mainpage::get_quick(5);
        $logo = Mainpage::get_image();
        $result_cart =  Mainpage::get_add_to_cart_details();

        $session_result = '';

       //            if(Session::get('cus_id'))
       //       {
          //        $navbar = View::make('layout.header');          
       //       }
       //       else
       //       {
       //           $navbar = View::make('layout.header');
       //       }
       return view('cart', ['logo' => $logo, 'quick'   =>$quick,'session_result'=>$session_result,'result_cart'=>$result_cart]);
          }

Model:

        public static function get_add_to_cart_details()
{
            $get_pro_dea = "";
            if(isset($_SESSION['cart'])){
            $max=count($_SESSION['cart']);
            for($i=0;$i<$max;$i++){
                $pid=$_SESSION['cart'][$i]['product_id'];
                //$pname="Have to get"; 
                $get_pro_dea[$pid] =  DB::table('le_product')->where('product_id',$pid)->get();         
            }
            }
            else
            {
                $get_pro_dea[0] = array();
            }
            return $get_pro_dea;
}

View:(header.blade.php):

  <div class="col-sm-4" style="margin-top:17px">
  <div class="cart box_1 pull-right">
    <a href="<?php echo url('cart'); ?>">
      <?php if(isset($_SESSION['cart']))
      {  
          $item_count_header1 = count($_SESSION['cart']); 

          } 

     else { 
         $item_count_header1 = 0; 

         }          

     $item_count_header = $item_count_header1;  
     if($item_count_header != 0) 
     { 
       ?>
        <img src='<?php echo url(); ?>/assets/images/shopping-cart.png'  alt=''><span  style='color:black;
      '>&nbsp;(&nbsp;<?php echo $item_count_header; ?>&nbsp;&nbsp; Items)  </span> 
          <?php

        }
     else 
        { ?>
           <img src="<?php echo url(); ?>/assets/images/shopping-cart.png" alt=""><span  style="color:black;">&nbsp;(card empty)</span>
     <?php }    
       ?>       
      </strong>

       </a>  


       </div>

       </div>

        </div>
         <?php } ?>
         </div>



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1VSUgmS
via IFTTT

Aucun commentaire:

Enregistrer un commentaire