I have input with multiple attribute, that allows multiple image uploads. However if it'll be a single image input, then i have no problems to store it's path in a database, but how to store multiple paths in a single column, something like how to insert an array in to a database?
You create a separate table in your database with fields id, product_id, image
$product = new Product();
$product->save(); // you need to save product before adding images
$images = $request->file('images');
foreach($images as $image)
{
$name = Str::lower(
pathinfo($image->getClientOriginalName(), PATHINFO_FILENAME) . '-' . uniqid() . '.' . $image->getClientOriginalExtension()
);
$images->move('uploads/', $name);
// this is the reason of saved product
$product->images()->create([
'image' => $name,
]);
}
@AlexanderKim from your create_products_table.php migration, you completely delete $table->string('image');, now create new migration create_product_images_table with id, product_id and image, and you leave images field required
class Product extends Model
{
public function images()
{
return $this->hasMany(ProductImage::class);
}
}
class ProductImage extends Model
{
public function product()
{
return $this->belongsTo(Product::class);
}
}
I just realized, why do i need a pivot table? I can create just OneToMany relation, right? I mean i don't need that pivot table, just a ProductImage table. Also i don't need that reverse relation.