---
title: Product
published: 2026-09-15
lastmod: 2026-11-02
url: https://larasell.dev/docs/product
---

## Table of contents
- [Creating a product](#creating-a-product)
- [Prices](#prices)
  - [Formatting prices](#formatting-prices)
  - [Compare-at prices](#compare-at-prices)
- [Visibility](#visibility)
- [Query products via categories](#query-products-via-categories)
- [Stock](#stock)
- [Translating the product](#translating-the-product)
- [Weights](#weights)
- [Dimensions](#dimensions)
- [SKUs and barcodes](#skus-and-barcodes)

# Product

## Creating a product

Use the `create` method to create a new product. A `Product`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Models/Product.php"} requires a `name` and a `price`:

```php
use Larasell\Larasell\Models\Product;

$product = Product::create([
    'name' => 'Bicycle',
    'price' => 1999,
]);
```

A product without a price is not useful, so Larasell won't let you create one.

## Prices

Products need prices, adding one is just one line:

```php
use Larasell\Larasell\Models\Product;

Product::create([
  'name' => 'Beer',
  'price' => 200,
]);
```

In addition, you can also pass a string or a `Price`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Price.php"} class:

```php
use Larasell\Larasell\Models\Product;
use Larasell\Larasell\Price;

Product::create([
  'name' => 'Pretzel',
  'price' => '100',
]);

Product::create([
  'name' => 'Sausage',
  'price' => Price::of(300),
]);
```

### Formatting prices

To format prices you need to pass a currency, either as a `Currency`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Enums/Currency.php"} enum or a simple string.

```php
use Larasell\Larasell\Enums\Currency;
use Larasell\Larasell\Price;

Price::format($product->price, 'EUR'); // '€12.99'
Price::format($product->price, Currency::USD); // '$12.99'
```

Prices look different depending on the locale.
For example, in German the currency symbol comes after the amount.

You can use the third parameter to format prices for different locales.

```php
use Larasell\Larasell\Enums\Currency;

Price::format($product->price, Currency::EUR, 'de-DE'); // '12,99 €'
Price::format($product->price, Currency::EUR, 'en-US'); // '€12.99'
```

### Compare-at prices

Compare-at prices make products look like they're on sale or
can make it look like customers are getting a better deal.

Set the `price` to the reduced rate — this is the price the customer will pay.
The `compare_at` price is displayed as the original price, making the product look like it's on sale.

```php
use Larasell\Larasell\Models\Product;

$product = Product::create([
    'name' => 'T-Shirt',
    'price' => Price::of(1299),
    'compare_at' => Price::of(1999)
]);
```

The `onSale` method makes it easy to show that a product is on sale.

```php
use Larasell\Larasell\Models\Product;

$product->onSale();
```

For the full price documentation, take a look at the [reference page](/docs/reference/price).

## Visibility

It's possible to define the visibility of a `Product`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Models/Product.php"}.
Common use cases are preparing for a new launch, or syncing products
from a different system and making them
available for purchase later.

```php
use Larasell\Larasell\Enums\Visibility;
use Larasell\Larasell\Models\Product;

$product = Product::create([
    'name' => 'Bicycle',
    'price' => 20_000,
    'status' => Visibility::Hidden,
]);
```

Then at a later point you can make it visible, and it will show up on the storefront.

```php
use Larasell\Larasell\Enums\Visibility;
use Larasell\Larasell\Models\Product;

$product = Product::where('name->en', 'Bicycle')->first();

$product->update([
  'status' => Visibility::Visible // or pass 'visible'
]);
```

A product can either be hidden or visible. If you want, you can just pass a string instead of passing the `Visibility`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Enums/Visibility.php"} enum.

Products have a `visible` query scope, meaning you can filter out hidden products with a single line of code.

```php
use Larasell\Larasell\Models\Product;

$products = Product::visible()->get();
```

> [!NOTE]
> It's not possible to add hidden products to a cart, so it's impossible to purchase a hidden product.

## Query products via categories

Often times you need to query products based on the category they belong to. `Product`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Models/Product.php"} has two methods to help you with that. `inCategory` gets you all the products that are part of a category:

```php
use Larasell\Larasell\Models\Category;
use Larasell\Larasell\Models\Product;

$category = Category::where('slug->en', 'clothing')->firstOrFail();

$products = Product::inCategory($category)->get();
```

This returns all the products that are part of the clothing category. But it won't
return products from sub-categories like `Clothing -> Shoes`. If you need to do that,
you can use the `inCategoryTree` method:

```php
use Larasell\Larasell\Models\Category;
use Larasell\Larasell\Models\Product;

$category = Category::where('slug->en', 'clothing')->firstOrFail();

$products = Product::inCategoryTree($category)->get();
```

Now you'll get all products that are inside `Clothing` and its sub-categories.

> [!NOTE]
> Both `inCategory` and `inCategoryTree` only return products from visible categories. If a category is hidden, its products won't be included.

To learn how to add and remove products from a category, take a look at the [category reference](/docs/reference/category#adding-products).

## Stock

By default, the product stock is `null`, which means customers can purchase this product indefinitely.
This is useful for digital products, but not for physical products.

To stock a product, set an integer value for `stock` on the Eloquent model:

```php
use Larasell\Larasell\Models\Product;

$product = Product::create([
    'name' => 'Poster',
    'price' => 1999,
    'stock' => 500,
]);

$variant = $product->defaultVariant();
$variant->availableStock(); // 500
```

By default, backorders are enabled for products. Backorders allow you to continue
selling a product even after it is out of stock.

To prevent this, set the value for `allow_backorders` to `false`:

```php {6,7,10,11}
use Larasell\Larasell\Models\Product;

$product = Product::create([
    'name' => 'Poster',
    'price' => 1999,
    'stock' => 500,
    'allow_backorders' => false,
]);

$variant = $product->defaultVariant();
$variant->allowsBackorders(); // false
```

Sometimes you want to make sure customers can only add a handful of products to a cart:

```php {6,7,10,11}
use Larasell\Larasell\Models\Product;

$socks = Product::create([
    'name' => 'Socks',
    'price' => 300,
    'stock' => 500,
    'min_quantity' => 6,
]);

$variant = $socks->defaultVariant();
$variant->minimumQuantity(); // 6
```

And other times you just want to make sure they cannot buy everything:

```php {6,7,10,11}
use Larasell\Larasell\Models\Product;

$handSanitizer = Product::create([
    'name' => 'Hand Sanitizer',
    'price' => 250,
    'stock' => 30,
    'max_quantity' => 4,
]);

$variant = $handSanitizer->defaultVariant();
$variant->maximumQuantity(); // 4
```

The `min_quantity` can never be larger than the `max_quantity`. If that's the case
Larasell will throw an exception:

```php {7,10,11}
use Larasell\Larasell\Models\Product;

$product = Product::create([
    'name' => 'Beer Crate',
    'price' => 250,
    'stock' => 30,
    'min_quantity' => 10,
]);

$product->max_quantity = 7; // Throws InvalidArgumentException
```

You don't have to enforce these limits yourself. When you add products to a
`Cart`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Models/Cart.php"},
Larasell checks the stock, backorder and quantity rules automatically and throws
a descriptive exception if a rule is violated.

## Translating the product

It's possible to translate certain fields of a product, like the name, slug and description.
Translations are keyed by [locale](/docs/reference/locale), written as a BCP 47 tag like `en`, `de`
or `de-DE`.

```php
use Larasell\Larasell\Models\Product;

Product::create([
  'name' => ['en' => 'Bicycle', 'de' => 'Fahrrad'],
  'price' => 200,
]);
```

To get the translated value, you call the `get` method on the product. It uses `App::currentLocale()`
to determine which translation to return to you.

```php
$product->name->get();
```

In case you want to get the translation for a specific locale, you can pass an optional string parameter
to the method.

```php
$product->name->get('de');
```

If the product has no translation for the locale, Larasell falls back to a shorter tag first,
so `de-AT` returns the `de` translation. After that it uses the shop's default locale from
`config/larasell/locale.php`, and finally the first translation the product has. The
[locale reference](/docs/reference/locale#fallback) explains the full order.

Translating an existing product is easy too.

```php
use Larasell\Larasell\Models\Product;

$bicycle = Product::where('slug->en', 'bicycle')->first();

$bicycle->update([
  'name' => ['nl' => 'Fiets']
]);
```

Note that calling `update` as above overwrites all previous translations. If you only want to add a translation,
you have to call the `with` method on the translatable value.

```php
$bicycle->update(['name' => $bicycle->name->with('fr', 'Vélo')]);
```

In this case, we added a translation for French. If it already existed, we would overwrite the value rather than add it.

The same is true for removing a translation. Call the `without` method to remove a single translation without
removing all translations.

```php
$bicycle->update(['name' => $bicycle->name->without('de')]);
```

The `all` method helps you see all the translations the product has.

```php
$bicycle->name->all(); // ['en' => 'Bicycle', 'nl' => 'Fiets', 'de' => 'Fahrrad']
```

There's a `has` method that you can call to check whether a product has a translation
for a specific locale.

```php
$bicycle->name->has('pt');
```

Locale keys are stored in their canonical form. Passing `de_DE` or `de-de` stores the translation
under `de-DE`, so `with`, `without`, `has` and `get` all accept either style.

The translatable fields on a product are `name`, `slug` and `description`. To serve a shop in
several languages, see the [localization guide](https://larasell.dev/docs/localization.md).

## Weights

You can define a weight and dimensions for a product. This is useful
to evaluate available shipping methods and their prices.

Use `Weight`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Weight.php"} to give a product a weight:

```php
use Larasell\Larasell\Enums\WeightUnit;
use Larasell\Larasell\Models\Product;
use Larasell\Larasell\Weight;

$product = Product::create([
  'name' => 'Bicycle',
  'price' => 20_000,
  'weight' => Weight::of('12.5', 'kg')
]);

$product->weight->amount(); // returns '12.5'

$product->weight->unit(); // returns WeightUnit::Kilogram
$product->weight->unit()->value; // returns 'kg'
```

It's possible to convert the weight for calculations or just for presentation:

```php
use Larasell\Larasell\Enums\WeightUnit;

$product->weight->to(WeightUnit::Pound)->amount(); // '27.55778277'
$product->weight->amount(WeightUnit::Ounce); // '440.9245243'
```

The above code does not update the weight on the product. The product still
uses the kilograms. To update the product you need to call the `to` method:

```php
use Larasell\Larasell\Enums\WeightUnit;

$product->update([
  'weight' => $product->weight->to(WeightUnit::Pound)
]);
```

## Dimensions

To add dimensions to products use `Dimensions`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Dimensions.php"}:

```php
use Larasell\Larasell\Dimensions;
use Larasell\Larasell\Models\Product;

$product = Product::create([
  'name' => 'Poster',
  'price' => 1999,
  'dimensions' => Dimensions::of(50, 35, 5, 'cm')
]);
```

Once you have a dimension you can get the length, width and height of a product:

```php
$product->dimensions->length(); // returns Length::of(50, 'cm')
$product->dimensions->length()->amount(); // '50'
$product->dimensions->length()->unit(); // returns LengthUnit::Centimeter

$product->dimensions->width(); // returns Length::of(35, 'cm')
$product->dimensions->height(); // returns Length::of(5, 'cm')
```

There's also a way to get the volume of a product:

```php
$product->dimensions->volume(); // returns '87500'; 50 × 35 × 5 cm³
```

To convert the dimensions to a different unit make use of the `to` method.
Keep in mind that `to` creates a new `Dimensions`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Dimensions.php"}
class and does not update the existing value, because the class is immutable.

```php
use Larasell\Larasell\Enums\LengthUnit;

$product->update([
  'dimensions' => $product->dimensions->to(LengthUnit::Millimeter)
]);

$product->dimensions->length()->amount(); // '500'
```

## SKUs and barcodes

Larasell allows you to add SKUs to a product:

```php
use Larasell\Larasell\Models\Product;

$product = Product::create([
  'name' => 'Bicycle',
  'price' => 1_000,
  'sku' => 'BIC-001'
]);

$sku = $product->sku;
```

A product can also have a barcode:

```php
use Larasell\Larasell\Models\Product;

$product = Product::create([
  'name' => 'Bicycle',
  'price' => 1_000,
  'barcode' => '4006381333931'
]);

$barcode = $product->barcode;
```

The barcode is stored as a string. If you want to check that it is a real
GTIN (EAN-8, UPC-A, EAN-13, or GTIN-14), wrap it in
`Barcode`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Barcode.php"}
first. The class strips spaces and hyphens. A missing or wrong check digit throws
an `InvalidArgumentException`.

```php
use Larasell\Larasell\Barcode;

Barcode::of('4006381333931');
Barcode::of('400 6381-333931'); // same digits
```
