@NDll

Как правильно реализовать корзину в Laravel?

есть класс который отвечает за работу корзины

<?php
namespace App\System\Library;
use App\Models\Cart;
use Illuminate\Support\Facades\DB;

class ShopCart
{
    private $data = array();

    private $session;

    public function __construct($session)
    {
$session = new ShopSession();
        $this->session = $session->getUserSessionIdFromCookies();
    }

    public function products(){
        $product_data = array();

        $cart_query = Cart::where('session_id','=', $this->session)->get();

        foreach ($cart_query as $cart) {
            $stock = true;

            $product_query = DB::table('products as p')
                ->leftJoin('product_descriptions as pd','pd.product_id','=', 'p.id')
                ->select('p.*','pd.name')
                ->where('p.id','=', $cart->product_id)
                ->where('pd.language_id','=', 1)
                ->where('p.published','=', 1)->first();

            if ($product_query && ($cart['quantity'] > 0)) {
                $price = $product_query->price;

                $product_data[] = array(
                    'quantity'        => $cart->quantity,
                    'total'           => $price * $cart->quantity
                );
            } else {
                $this->remove($cart->d);
            }
        }

        return $product_data;
    }

    public function add($product_id){
        $cart = Cart::where('session_id','=', $this->session)
            ->where('product_id','=', $product_id)->first();

        if ($cart){
            $cart->update([
                'quantity' => $cart->quantity + 1
            ]);
        }else{
            $cart = new Cart();
            $cart->user_id = 0;
            $cart->product_id = $product_id;
            $cart->session_id = $this->session;
            $cart->save();
        }

        return $cart->quantity;
    }

    public function total() {
        $product_total = 0;

        $products = $this->products();

        foreach ($products as $product) {
            $product_total += $product['quantity'];
        }

        return $product_total;
    }

    public function remove($cart_id){
        ShopCart::find($cart_id)->delete();
    }
}


класс с сессиями

<?php
namespace App\System\Library;
use Illuminate\Support\Facades\Cookie;
class ShopSession
{
    public function getUserSessionIdFromCookies(){
        if (Cookie::get('session_id'))
            $session_id = Cookie::get('session_id');
        else{
            Cookie::put('session_id', substr(bin2hex(random_bytes(26)), 0, 26), time() + 3600*24*30*365);
            $session_id = Cookie::get('session_id');
        }
        return $session_id;
    }
}


при добавлении товара в контроллере

class CartController extends Controller
{
    protected function put(CartRequest $request){
        $cart = new ShopCart();
        $cart->addProductToCart((int)$request->itemId);
        $total = $cart::countProducts();
    }
}


выдает

Non-static method App\System\Library\ShopCart::countProducts() should not be called statically
  • Вопрос задан
  • 361 просмотр
Пригласить эксперта
Ответы на вопрос 1
@MaLuTkA_UA
А что непонятного у тебя в классе ShopCart нету статической функции countProducts(). Или пользоваться переводом если не знаешь английский уже не модно?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы