0

I am using the Laravel cart package that I got from here

I have followed the instruction to install the package. Here is the Cart.php model class:

namespace App;

use Amsgames\LaravelShop\Models\ShopCartModel;

class Cart extends ShopCartModel
{

}

The ShopCartModel

use Amsgames\LaravelShop\Contracts\ShopCartInterface;
use Amsgames\LaravelShop\Traits\ShopCartTrait;
use Amsgames\LaravelShop\Traits\ShopCalculationsTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;

class ShopCartModel extends Model implements ShopCartInterface
{

    use ShopCartTrait, ShopCalculationsTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table;

    /**
     * Fillable attributes for mass assignment.
     *
     * @var array
     */
    protected $fillable = ['user_id','user_count', 'user_price'];

    /**
     * Creates a new instance of the model.
     *
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->table = Config::get('shop.cart_table');
    }

}

and finally the ShopCartInterface

namespace Amsgames\LaravelShop\Contracts;

/**
 * This file is part of LaravelShop,
 * A shop solution for Laravel.
 *
 * @author Alejandro Mostajo
 * @copyright Amsgames, LLC
 * @license MIT
 * @package Amsgames\LaravelShop
 */

interface ShopCartInterface
{

    /**
     * One-to-One relations with the user model.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function user();

    /**
     * One-to-Many relations with Item.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function items();

    /**
     * Adds item to cart.
     *
     * @param mixed $item     Item to add, can be an Store Item, a Model with ShopItemTrait or an array.
     * @param int   $quantity Item quantity in cart.
     */
    public function add($item, $quantity = 1, $quantityReset = self::QUANTITY_ADDITION);

    /**
     * Removes an item from the cart or decreases its quantity.
     * Returns flag indicating if removal was successful.
     *
     * @param mixed $item     Item to remove, can be an Store Item, a Model with ShopItemTrait or an array.
     * @param int   $quantity Item quantity to decrease. 0 if wanted item to be removed completly.
     *
     * @return bool
     */
    public function remove($item, $quantity = 0);

    /**
     * Checks if the user has a role by its name.
     *
     * @param string|array $name       Role name or array of role names.
     * @param bool         $requireAll All roles in the array are required.
     *
     * @return bool
     */
    public function hasItem($sku, $requireAll = false);

    /**
     * Scope to current user cart and returns class model.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query  Query.
     *
     * @return this
     */
    public function scopeCurrent($query);

    /**
     * Returns total amount of items in cart.
     *
     * @return int
     */
    public function getCountAttribute();

    /**
     * Returns total price of all the items in cart.
     *
     * @return float
     */
    public function getTotalPriceAttribute();

    /**
     * Returns total tax of all the items in cart.
     *
     * @return float
     */
    public function getTotalTaxAttribute();

    /**
     * Returns total tax of all the items in cart.
     *
     * @return float
     */
    public function getTotalShippingAttribute();

    /**
     * Returns total discount amount based on all coupons applied.
     *
     * @return float
     */
    public function getTotalDiscountAttribute();

    /**
     * Returns total amount to be charged base on total price, tax and discount.
     *
     * @return float
     */
    public function getTotalAttribute();

    /**
     * Returns formatted total price of all the items in cart.
     *
     * @return string
     */
    public function getDisplayTotalPriceAttribute();

    /**
     * Returns formatted total tax of all the items in cart.
     *
     * @return string
     */
    public function getDisplayTotalTaxAttribute();

    /**
     * Returns formatted total tax of all the items in cart.
     *
     * @return string
     */
    public function getDisplayTotalShippingAttribute();

    /**
     * Returns formatted total discount amount based on all coupons applied.
     *
     * @return string
     */
    public function getDisplayTotalDiscountAttribute();

    /**
     * Returns formatted total amount to be charged base on total price, tax and discount.
     *
     * @return string
     */
    public function getDisplayTotalAttribute();

    /**
     * Transforms cart into an order.
     * Returns created order.
     *
     * @param mixed $status Order status to create order with.
     *
     * @return Order
     */
    public function placeOrder($statusCode = null);

    /**
     * Whipes put cart
     */
    public function clear();

}

and what I tried to do is to add the item to the shopping cart in the following codes

$items = DB::table('apps as a')
                        ->leftJoin('odoo_modules as b', 'a.i_odoo', '=', 'b.id')
                        ->whereIn('a.id', Input::get('items'))
                        ->select('a.id as i_apps', 'b.name as sku', 'a.price')
                        ->get();

            $cart = \App\Cart::current();

            foreach($items as $item){
                $cart->add(['sku' => $item->sku, 'i_apps' => $item->i_apps, 'price' => $item->price]);
            }

And I got this error

BadMethodCallException in Builder.php line 2345:
Call to undefined method Illuminate\Database\Query\Builder::add()

Do I have to implement the add method in the model Cart ? If not what causes the error ?

Priska Aprilia
  • 1,149
  • 1
  • 15
  • 34
  • Did you try this as guest or logged user? Because according to this https://github.com/amsgames/laravel-shop/issues/30 and `current()` method implimentation on ShopCartTrait.php you can't use this with guest users. – Ravisha Hesh Jun 08 '16 at 16:15
  • It is a logged user. I use Cartalyst Sentinel to authenticate the user credentials, however after redirecting to other Controller and try to echo `Sentinel::check()`, the function always returns false. It means the issue lies on the Cartalyst Sentinel (or the logic) for not detecting the logged user. – Priska Aprilia Jun 09 '16 at 02:48
  • If you are going to use Cartalyst Sentinel you have to override the `scopeCurrent` method on ShopCartTrait.php(line:219) in your `ShopCartModel ` to use `Sentinel::check()` and `Sentinel::getUser()` instead of `Auth` methods. (Trait methods overriding: http://stackoverflow.com/questions/11939166/how-to-override-trait-function-and-call-it-from-the-overriden-function) – Ravisha Hesh Jun 09 '16 at 04:27
  • The Sentinel::check() keeps returning false even for a logged user. But your insights about overriding the existing methods indeed helped me. I managed to solve my problem by using the session variable instead of Sentinel. Anyway thank you – Priska Aprilia Jun 09 '16 at 07:06

0 Answers0