Get image of all associated product of a configurable product

Get image of all associated product of a configurable product

October 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get image of all associated product of a configurable product

Displaying Associated Product Images for Configurable Products in Laravel

When working with configurable products in an e-commerce application built using Laravel, you might need to display images of all the associated simple products along with their options. This can be achieved by leveraging the Eloquent relationships and querying the catalog_product_entity_media_gallery table directly.

Understanding the Configuration

In a typical Magento setup, a configurable product is linked to multiple simple products (associations) that share some attributes but have different price or SKU values. The images for these simple products are stored in the media_gallery table.

Retrieving Associated Product Images

To fetch all associated simple products and their related images for a given configurable product, you can use the following approach. First, ensure your models are properly set up with the necessary relationships.

// In your SimpleProduct Model (App\Models\SimpleProduct.php)
public function configurable_parent()
{
    return $this->belongsTo(ConfigurableProduct::class);
}

Next, in your controller or repository method, you can write a query to fetch all simple products associated with a specific configurable product and their gallery images.

use App\Models\ConfigurableProduct;
use Illuminate\Support\Facades\DB;

// Fetch the configurable product
$configurable_product = ConfigurableProduct::find(1);

// Fetch all associated simple products along with media_gallery
$simple_products_with_images = DB::table('catalog_product_entity')
    ->select(['catalog_product_entity.*', 'mg.file'])
    ->join('catalog_product_super_attribute', 'catalog_product_entity.entity_id', '=', 'catalog_product_super_attribute.product_id')
    ->join('catalog_product_super_link', function ($join) use ($configurable_product) {
        $join->on('catalog_product_entity.entity_id', '=', 'catalog_product_super_link.product_id');
        $join->where('catalog_product_super_link.parent_id', '=', $configurable_product->entity_id);
    })
    ->leftJoin('catalog_product_entity_media_gallery as mg', function ($join) {
        $join->on(DB::raw('catalog_product_entity.entity_id'), '=', 'mg.entity_id');
        $join->where('mg.attribute_id', 61); // Media gallery attribute ID for product image
    })
    ->get();

This query returns a collection of simple products where each item has the file column containing the file name of the associated product image.

Displaying Images in Your View

In your Blade template, you can loop through this collection and display the images using Laravel’s built-in HTML helper functions:

@foreach ($simple_products_with_images as $product)
    <img src="{{ asset('media/catalog/product/' . $product->file) }}" alt="{{ $product->name }}">
@endforeach

Conclusion

Displaying associated product images for configurable products in a Laravel application requires understanding and querying the correct database relationships. By leveraging Eloquent and the direct query builder, you can efficiently fetch all necessary data to display these images alongside your products.

Remember, these queries assume you have already set up your Magento database schema with proper migrations and models in your Laravel project. If you haven’t done so, consider using a package like Laravel Magento for a more integrated solution.

Implementing this approach ensures that your configurable products’ images are displayed correctly, enhancing the user experience in your e-commerce storefront.

Catalog Database Configuration Pricing

Our network