PHP code example of divineomega / laravel-extendable-basket

1. Go to this page and download the library: Download divineomega/laravel-extendable-basket library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

divineomega / laravel-extendable-basket example snippets


# app/Basket.php


namespace App;

use DivineOmega\LaravelExtendableBasket\Models\Basket as BasketModel;

class Basket extends BasketModel
{
    public function items(): HasMany
    {
        return $this->hasMany('App\BasketItem');
    }
}

# app/BasketItem.php


namespace App;

use DivineOmega\LaravelExtendableBasket\Models\BasketItem as BasketItemModel;

class BasketItem extends BasketItemModel
{
    public function basket(): BelongsTo
    {
        return $this->belongsTo('App\Basket');
    }
}

# app/Product.php


namespace App;

use Illuminate\Database\Eloquent\Model;
use DivineOmega\LaravelExtendableBasket\Interfaces\Basketable;

class Product extends Model implements Basketable {

    // ...

}

$basket = Basket::getCurrent();

$basket = Basket::getNew();

// Get new basket
$basket = new Basket();

// Get current basket
$userBasketId = Auth::user()->basket_id;
$basket = Basket::find($userBasketId);

$quantity = 5;
$product = Product::FindOrFail(1);

$basket->add($quantity, $product);
$basket->add($quantity, $product, ['colour' => 'red', 'size' => 'medium']);

foreach($basket->items as $item) {
    $product = $item->basketable;
    echo $product->name;
}

$item = $basket->items->first();
$item->setQuantity($request->quantity);

$item = $basket->items->first();
$item->delete();

$item = $basket->items->first();
$item->basketable->getPrice();

$item = $basket->items->first();
$item->getPrice();

$numItems = $basket->getTotalNumberOfItems();

$subtotal = $basket->getSubtotal()

# app/Basket.php


namespace App;

use DivineOmega\LaravelExtendableBasket\Models\Basket as BasketModel;

class Basket extends BasketModel
{
    public function items()
    {
        return $this->hasMany('App\BasketItem');
    }

    public function getTotal()
    {
        $deliveryCost = 3.99;

        return $this->getSubtotal() + $deliveryCost;
    }
}

php artisan migrate