kendrick's avatar

Product Creation: Multiple Image Upload (Database Connection)

Hello,

I am currently working on a Product Create Part, to be associated with an Authenticated User.

Question: When I use one input field and allow multiple upload, how am I supposed to store all files within the database attached to the correct product?

E.g. while creating a product as an Authenticated Business. So, I edit the content such as Title, Brand and Price and want to add 5 pictures to it.

Products table:

public function up()
    {
        Schema :: create ('products', function(Blueprint $table){
            $table->increments('id');
            $table->unsignedInteger('business_id');
            $table->string ('title')->nullable();
            $table->string ('brand')->nullable();
            $table->decimal ('price')->nullable();
            $table->string('product_image')->default('productdefault.jpg');
            $table->timestamps();
    });

ProductUploadController.php:

public function postProductEdit(Request $request){
        
        $this->validate($request, [

            'title'=> 'max:140',
            'brand'=> 'max:140',
            'price'=> 'max:10',
            'product_image'=> 'max:140',

        ]);

        
        $update = Auth::user()->products()->create([
            'business_id' => auth()->user()->id,
            'title' => $request->title,
            'brand' => $request->brand,
            'price' => $request->price,
            'product_image' => $request->product_image,
            
        ]);

        if($request->hasFile('product_image')){

            $business = Auth::user();
            $image   = $request->file('product_image');         
            $filename = time() . '.' . $image->getClientOriginalExtension();            
            $location = public_path('uploads/business/products/'. $filename );          
            Image::make($image)->resize(812, null, function ($constraint){$constraint->aspectRatio();})->save($location);
            
            $business->product_image = $filename;
            $business->save();  
        


        return redirect()
            ->route('products.create')
            ->with('info', 'Your product has been created.');
    }

At the moment, it won't work. Would be happy about your expertise. Thank you

0 likes
4 replies
zion's avatar
zion
Best Answer
Level 9

Have a look at a one-to-many relationship between your product and images. https://laravel.com/docs/5.5/eloquent-relationships#one-to-many

You'll want to setup another table for your images:

public function up()
    {
        Schema :: create ('product_images', function(Blueprint $table){
            $table->increments('id');
            $table->unsignedInteger('product_id'); // <-- Relation to the product.
            $table->string('product_image')->default('productdefault.jpg');
            $table->timestamps();
    });

Products model:

public function images()
{
      return $this->hasMany(Image::class);
}
1 like
kendrick's avatar

Thank you @zion

I understand. But I am just wondering, is there a possibility that the DB gets dynamic, like based on the number of images uploaded by the User, it will create columns at this amount.

Or do I have to limit it by only providing 5 images to be uploaded because I only have 5 columns within the DB, like image_1, image_2 and so on.

code_chris's avatar

You don't make new columns, you have a new table and you add a row for each image which is linked to the correct product id.

In this way you can have many images without adjusting database structure in future.

1 like

Please or to participate in this conversation.