لايف واير

بناء سلة تسوق مع Livewire v4

February 01, 2026 1 دقيقة قراءة 20 مشاهدة

ابنِ سلة تسوق مع Livewire.

مكون السلة

@php
new class extends Livewire\Component {
    public array $items = [];
    
    public function addItem(int $productId): void
    {
        $product = Product::find($productId);
        $this->items[$productId] = [
            'name' => $product->name,
            'price' => $product->price,
            'qty' => ($this->items[$productId]['qty'] ?? 0) + 1,
        ];
    }
    
    #[Computed]
    public function total(): float
    {
        return collect($this->items)->sum(fn($i) => $i['price'] * $i['qty']);
    }
}
@endphp
شارك هذه المقالة:

مقالات ذات صلة

التعليقات (0)

يرجى تسجيل الدخول لترك تعليق. تسجيل الدخول

لا توجد تعليقات بعد. كن أول من يعلق!