Basket::save PHP Method

save() public method

Save current item
public save ( ) : array
return array
    function save()
    {
        if (!$this->id) {
            $this->id = uniqid(NULL, TRUE);
        }
        $_SESSION[$this->key][$this->id] = $this->item;
        return $this->item;
    }

Usage Example

Esempio n. 1
0
    $basket = Basket::where('sessionId', '=', Session::getId())->first();
    return View::make('index')->with('ps', $ps)->with('basket', $basket);
});
Route::post('add-to-basket', ['as' => 'add.to.basket', function () {
    //$id    = Input::get('id');
    //$count = Input::get('count', 1);
    // create or find old basket
    $basket = Basket::where('sessionId', '=', Session::getId())->first();
    if (!$basket) {
        $basket = new Basket();
        $basket->price = 0;
        $basket->productCount = 0;
        $basket->status = Basket::STATUS_UNPAYED;
        $basket->date = date('Y-m-d H:i:s');
        $basket->sessionId = Session::getId();
        $basket->save();
    }
    // find prodcudt
    $product = Product::find(Input::get('id'));
    // add product to pasket products
    $bp = new BasketProduct();
    $bp->basketId = $basket->id;
    $bp->productId = $product->id;
    $bp->count = Input::get('count');
    $bp->price = $product->price;
    $bp->save();
    //
    $row = DB::table('basket_product')->select(DB::raw('SUM(price) as p'), DB::raw('SUM(count) as c'))->where('basketId', '=', $basket->id)->first();
    //
    $basket->price = $row->p;
    $basket->productCount = $row->c;