Sep 8, 2025
0
Level 1
Filament Relationships " saveRelationshipsUsing() " for Product Images.
I want to add the product images to a separate table like:
Tables: products product_images
I already created the relationship between the product and product images models.
App/Models/Product.php:
/**
* Get the images for the product.
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function product_images(): HasMany
{
return $this->hasMany(ProductImage::class);
}
App/Models/ProductImage.php:
/**
* Get the product that owns the image.
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
I am adding the product featured image to the same products table, and the rest of the images like product gallery images to the product_images table.
Not achieveing the desired output from the code i did into the ProductResource.php
ProductResouce.php
FileUpload::make('thumbnail')
->label('Featured Image')
->image()
->imageEditor()
->imageEditorAspectRatios([
null,
'16:9',
'4:3',
'1:1',
])
->directory('products/featured')
->required(),
FileUpload::make('product_images')
->label('Gallery Images')
->multiple()
->image()
->imageEditor()
->imageEditorAspectRatios([
null,
'16:9',
'4:3',
'1:1',
])
->directory('products/gallery')
->saveRelationshipsUsing(
fn($record, $state) => $record->product_images()->createMany(
array_map(fn($image) => ['image' => $image], $state)
)
),
Please or to participate in this conversation.