mercredi 8 juin 2016

Laravel BadMethodCallException upon calling a method in an interface

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 ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire