Skip to content

Stock Badge

Colored badge that shows a stock quantity, red below 10 units and green from 10.

The stock badge shows a stock quantity in a small rounded badge, colored by level. The products table uses it in its stock column.

Preview

The preview shows a low quantity and two normal ones.

525100

Usage

Pass the quantity to the stock prop:

<x-shopper::stock-badge :stock="$product->stock" />

Props

The badge takes a single prop and no slot. It ignores other attributes.

PropDefaultDescription
x-shopper::stock-badgerenders a <span> element
stock
int
required

The quantity to display.

Behavior

The color depends on the quantity. The threshold of 10 units is fixed in the view: to change it, override the view.

Stock valueColorMeaning
< 10Red (danger)Low stock
>= 10Green (success)Stock OK

Examples

The examples below show both colors and the badge in a table column.

Low Stock

A quantity under 10 renders red.

3

This Blade passes a stock of 3:

<x-shopper::stock-badge :stock="3" />

Normal Stock

A quantity of 10 or more renders green.

50

This Blade passes a stock of 50:

<x-shopper::stock-badge :stock="50" />

In a Table

In a Filament table, render the badge from a ViewColumn. This column renders the stock cell of each row with a Blade view:

use Filament\Tables\Columns\ViewColumn;

ViewColumn::make('stock')
    ->label('Stock')
    ->view('livewire.tables.cells.stock')

The view reads the product of the row through $getRecord() and shows its stock in the badge, followed by "in stock":

resources/views/livewire/tables/cells/stock.blade.php
@php
    $product = $getRecord();
@endphp

<div class="flex items-center">
    <x-shopper::stock-badge :stock="$product->stock" />
    <span class="text-sh-fg-secondary text-sm/6">in stock</span>
</div>

Shopper's products table renders its stock column the same way, with the view shopper::livewire.tables.cells.products.stock.

Blade Component Source

The view is resources/views/components/stock-badge.blade.php in the shopper/framework package:

@blaze(memo: true)

@props([
    'stock',
])

<span
    @class([
        'me-2 inline-flex rounded-full px-1.5 text-xs leading-5 font-semibold',
        'bg-danger-100 text-danger-800' => $stock < 10,
        'bg-success-100 text-success-800' => $stock >= 10,
    ])
>
    {{ $stock }}
</span>

Last updated on

Edit on GitHub