Add a badge for stock
This commit is contained in:
@@ -11,7 +11,10 @@ final class IndexController extends BaseController
|
||||
#[Route('/', name: 'app_home')]
|
||||
public function __invoke(): Response
|
||||
{
|
||||
$products = Product::with('price')->orderByDesc('starred')->orderByDesc('created_by')->get();
|
||||
$products = Product::with(['currentStock', 'price'])
|
||||
->orderByDesc('starred')
|
||||
->orderByDesc('created_by')
|
||||
->get();
|
||||
return $this->render('productList.html.twig', ['products' => $products]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,13 @@ class Product extends Model
|
||||
return $this->hasMany(Stock::class);
|
||||
}
|
||||
|
||||
public function currentStock(): HasOne
|
||||
{
|
||||
return $this->stock()->one()->ofMany()->withDefault(function (Stock $stock) {
|
||||
$stock->stock = 0;
|
||||
});
|
||||
}
|
||||
|
||||
public function toggleStarred(): self
|
||||
{
|
||||
$this->starred = !$this->starred;
|
||||
|
||||
Reference in New Issue
Block a user