Skip to main content

Product images

Creating images

You can add images to products in two simple steps. First, create a new ProductImage and second, add it to the product.

 1use Larasell\Larasell\Models\Product;
 2use Larasell\Larasell\Models\ProductImage;
 3
 4$product = Product::query()->where('slug->en', 'bicycle')->first();
 5
 6$image = ProductImage::create([
 7  'file' => $request->file('image'),
 8  'alt' => 'Bicycle from the side',
 9]);
10
11$product->images()->attach($image, [
12  'position' => 0,
13]);

Getting images

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

1$product->images;

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

1use Larasell\Larasell\Models\Product;
2
3$product = Product::query()
4    ->with('images')
5    ->where('slug->en', 'bicycle')
6    ->first();

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

1foreach ($product->images as $image) {
2    $image->url();
3    $image->alt;
4}

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

1$product->thumbnail;

Position

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

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

If you ever need to update the position you can call Laravel's updateExistingPivot method:

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

Placeholders

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

1use Larasell\Larasell\Images\LqipPlaceholderGenerator;
2
3'images' => [
4  'placeholder' => LqipPlaceholderGenerator::class,
5],

There are two ways to generate placeholders:

LqipPlaceholderGenerator creates a very small and low quality version of your image and stores it as a URI string.

Use ColorPlaceholderGenerator 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.

Displaying placeholders

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

 1use Larasell\Larasell\Models\Product;
 2
 3class ProductController
 4{
 5    public function show(string $slug)
 6    {
 7        $product = Product::query()
 8            ->with('images')
 9            ->where('slug->en', $slug)
10            ->firstOrFail();
11
12        return view('products.show', [
13            'product' => $product,
14            'images' => $product->images->map(fn ($image) => [
15                'url' => $image->url(),
16                'alt' => $image->alt,
17                'placeholder' => $image->placeholder?->toArray(),
18            ]),
19        ]);
20    }
21}

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 /> component from the Inertia starter kit.

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

 1Help me display placeholders for Larasell. Take a look at the official Larasell documentation
 2about image placeholders: https://larasell.test/docs/product-images.md
 3
 4For 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)
 5
 6If possible, create a new component that encapsulates the new behaviour. Ideally,
 7the component does not have any styles and just contains the behaviour. The parent
 8component should style the elements. This makes it easier to show placeholders
 9in different places.
10
11Once 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:

1php 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.