---
title: Product images
published: 2026-09-21
lastmod: 2026-09-21
url: https://larasell.dev/docs/product-images
---

## Table of contents
- [Creating images](#creating-images)
- [Getting images](#getting-images)
- [Position](#position)
- [Placeholders](#placeholders)
  - [Displaying placeholders](#displaying-placeholders)
  - [Changing the placeholder generator](#changing-the-placeholder-generator)
- [Configuring storage](#configuring-storage)

# Product images

## Creating images

You can add images to products in two simple steps. First, create a new
`ProductImage`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Models/ProductImage.php"}
and second, add it to the product.

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

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

$image = ProductImage::create([
  'file' => $request->file('image'),
  'alt' => 'Bicycle from the side',
]);

$product->images()->attach($image, [
  'position' => 0,
]);
```

## Getting images

Images belong to products. You can get them like this:

```php
$product->images;
```

In most cases the best way to get images is when you eager-load
them using the `with` method:

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

$product = Product::query()
    ->with('images')
    ->where('slug->en', 'bicycle')
    ->first();
```

Once you have the images you can get the URL and an alt text:

```php
foreach ($product->images as $image) {
    $image->url();
    $image->alt;
}
```

There's a `thumbnail` property to get the first image of a product:

```php
$product->thumbnail;
```

## Position

In many cases you need to define in which order to show product
images to a customer:

```php
$product->images()->attach($image, [
  'position' => 0,
]);
```

If you ever need to update the position you can call Laravel's
`updateExistingPivot`{href="https://laravel.com/framework/docs/eloquent-relationships#updating-a-record-on-the-intermediate-table"} method:

```php
$product->images()->updateExistingPivot($image->getKey(), [
  'position' => 2,
]);
```

## Placeholders

Larasell does not generate placeholders by default. To enable it, set the
placeholder generator in `config/larasell.php`:

```php
use Larasell\Larasell\Images\LqipPlaceholderGenerator;

'images' => [
  'placeholder' => LqipPlaceholderGenerator::class,
],
```

There are two ways to generate placeholders:

`LqipPlaceholderGenerator`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Images/LqipPlaceholderGenerator.php"} creates a very small and low quality version of your
image and stores it as a URI string.

Use `ColorPlaceholderGenerator`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/src/Images/ColorPlaceholderGenerator.php"}
if you only want to display a single color. The generator extracts the most used
color from that image and stores it as a hex code.

> [!NOTE]
> Larasell does not generate placeholders for SVGs.

### Displaying placeholders

Once you have a placeholder for an image you can pass it to the frontend in your controller:

```php{17-17}
use Larasell\Larasell\Models\Product;

class ProductController
{
    public function show(string $slug)
    {
        $product = Product::query()
            ->with('images')
            ->where('slug->en', $slug)
            ->firstOrFail();

        return view('products.show', [
            'product' => $product,
            'images' => $product->images->map(fn ($image) => [
                'url' => $image->url(),
                'alt' => $image->alt,
                'placeholder' => $image->placeholder?->toArray(),
            ]),
        ]);
    }
}
```

Now you can access the data on the frontend. How you display that data depends
on what you're using to build the UI. The easiest way is to use the `<Image />`{href="https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/stubs/starter-kit/resources/js/Components/Image.tsx"}
component from the [Inertia starter kit](./starter-kit).

If you're not using that, this prompt might help you out:

```markdown
Help me display placeholders for Larasell. Take a look at the official Larasell documentation
about image placeholders: https://larasell.test/docs/product-images.md

For inspiration you can take a look at the implementation for [React.js](https://raw.githubusercontent.com/Larasell-dev/larasell/refs/heads/main/stubs/starter-kit/resources/js/Components/Image.tsx)

If possible, create a new component that encapsulates the new behaviour. Ideally,
the component does not have any styles and just contains the behaviour. The parent
component should style the elements. This makes it easier to show placeholders
in different places.

Once you've done that, show me how to use this new component.
```

### Changing the placeholder generator

If you ever decide to change the way you generate placeholders or you want
to start generating placeholders, you need to update the placeholders for
existing product images.

This is because the generators create placeholders when you _create_ new product images.

Run the command below to update the placeholders for all existing images:

```bash
php artisan larasell:refresh-placeholders
```

## Configuring storage

By default, Larasell stores images on the same disk that Laravel
uses for all other files. If you ever want to change that, you can
update the `FILESYSTEM_DISK` environment variable to any of the
drivers you configured.

In case you want to separate the product images from all other
files, you have to use the `LARASELL_IMAGES_PATH` env variable.
