lundi 27 novembre 2017

Laravel 5.4 shopping cart with two model

i have a problem with my shopping cart ... In my project, i have two models différents :Product 1('Gammes') and Product 2('Livres').

i want to increment when i click on the link...

/**
 * Create a new controller instance.
 *
 */
public function __construct()
{
    $cart=[];
    if(!\Session::has('panier'))\Session::put('panier',$cart);



}

My function add is this :

 public function addGammes($id)
{



    $gammes = Gammes::findOrFail($id);



    $oldCart = \Session::has('panier') ? \Session::get('panier') : null;


    //dd($oldCart);
    if($oldCart== null){
        $cart = \Session::get('panier');
        $gammes->quantity=1;
    $cart[$gammes->id]=$gammes;



    \Session::put('panier',$cart);
    }
    else{
        $cart = \Session::get('panier');
        if($cart[$gammes->id] != null){
            $cart = \Session::get('panier');

            \Session::put('panier',$cart,$cart[$gammes->id]->quantity+=1);
        }
        else{
            $cart = \Session::get('panier');
            $gammes->quantity=1;
            $cart[$gammes->id]=$gammes;



            \Session::put('panier',$cart);
        }

//dd(\Session::get('panier'));

    }

  // dd($cart[$gammes->id]);




 // dd($cart[$gammes->id]->quantity);

    flash('Gammes ajoute au panier')->success();
    return back();

}
public function addLivres($id)
{


    $article=Produit::findOrFail($id);
    $cart= \Session::get('panier');
    $article->quantity+=1;
    $cart[$article->id]=$article;
    \Session::put('panier',$cart);



    flash('Produit ajoute au panier')->success();
    return back();
}

I would like verify the session and added the product in my array :s



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire