ddaniel's avatar

Problem with adding image to product

HI. I'm new in Laravel 5.7. I have a problem with adding the image to the product. I think the problem is related with file path and all "$request->hasFile" but I can't solve it :-/ I have tried many times with various examples that I found. I have installed Intervention Image package too.

    public function store(Request $request)  // POST
    {
        $attributes = request()->validate([
            'product_name' => ['required', 'min:3', 'max:60'],
            'product_code' => ['nullable', 'numeric'],
            'product_price' => ['required', 'min:1', 'max:8'],
            'id_subcategory' => ['required'],
            'product_thumbnail' => ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg|max:2048'],
            'product_short_specification',
            'product_description' => ['required']
        ]);
            
        if($request->hasFile('product_thumbnail')){
            $image = $request->file('product_thumbnail');
            $filename = time().'.'.$image->getClientOriginalName();
            Storage::put($filename, 'images/product_images/');
         }

        Product::create($attributes);
        return redirect('admin/products');
    }

When I submit form in my database in the table "products", in column "product_thumbnail" i can see paths like ...\xampp\tmp\phpC257.tmp and image is not uploaded to destination folder "images/product_images/".

I will be grateful for help.

0 likes
6 replies
Dalma's avatar

Can you also post your Blade Form that is submitting this request?

I'm not sure if this is an issue but you may have issues with permissions on your images/product_images path?

ddaniel's avatar
<!-- form/start -->
                <form method="POST" action='{{ route('products.store')  }}' enctype="multipart/form-data"> 
                    {{ csrf_field() }}

                    <div class="form-row">
                        <div class="col-12">
                            <div class="form-group">
                                 <label for="productinput01">Nazwa produktu</label>
                                 <input class="form-control {{ $errors->has('product_name') ? 'is-invalid' : '' }}" value="{{ old('product_name') }}" required name="product_name" type="text" id="productinput01" placeholder="--- podaj nazwę ---">
                            </div>
                        </div>
                        <div class="col-4">
                            <div class="form-group">
                                 <label for="productinput02">Kod produktu</label>
                                 <input class="form-control {{ $errors->has('product_code') ? 'is-invalid' : '' }}" value="{{ old('product_code') }}" name="product_code" type="number" id="productinput02" placeholder="--- podaj kod ---">
                            </div>
                        </div>
                        <div class="col-3">
                            <div class="form-group">
                                <label for="productinput03">Cena</label>
                                <input class="form-control {{ $errors->has('product_price') ? 'is-invalid' : '' }}" value="{{ old('product_price') }}" required name="product_price" id="productinput03" placeholder="--- podaj cenę ---">
                            </div>
                        </div>
                        <div class="col-4 offset-1">
                            <div class="form-group">
                                <label for="formGroupExampleInput">Dostępność produktu</label>
                                <div class="custom-switch custom-switch-label-yesno p-0">
                                    <input class="custom-switch-input" id="example_3" type="checkbox">
                                    <label class="custom-switch-btn float-left mr-2" for="example_3"></label>
                                    <div class="custom-switch-content-checked custom-switch-preferences">
                                        <span class="text-success">Produkt dostępny</span>
                                    </div>
                                    <div class="custom-switch-content-unchecked custom-switch-preferences">
                                        <span class="text-danger">Produkt niedostępny</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="form-row mt-3">
                        <div class="col-6 ">
                            <div class="form-group">
                            <label for="category">Kategoria</label>
                            <select class="form-control" name="id_category" id="category">
                                    <option selected disabled>--- wybierz kategorię ---</option>
                                @foreach ($categories as $category)
                                    <option value="{{ $category->id_category }}">
                                        {{ $category->category_name }}
                                    </option>
                                @endforeach
                            </select>
                            </div>
                        </div>  
                        <div class="col-6">
                            <div class="form-group">
                                <label for="subcategory">Podkategoria</label>
                                <select class="form-control" name="id_subcategory" id="subcategory" title="Wybierz podkategorię...">
                                        <option selected disabled>
                                                --- wybierz podkategorię ---
                                        </option>
                                    @foreach ($subcategories as $subcategory)
                                        <option value="{{ $subcategory->id_subcategory }}">
                                            {{ $subcategory->subcategory_name }}  
                                        </option>
                                    @endforeach

                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="form-row mt-3">
                            <div class="col-12">
                                    <div class="form-group">
                                            <label for="product_short_specification">Krótka specyfikacja produktu</label>
                                            <textarea class="form-control {{ $errors->has('product_short_specification') ? 'is-invalid' : '' }}" name="product_short_specification" id="product_short_specification" rows="8">{{ old('product_short_specification') }}</textarea>
                                    </div>
                            </div>
                        <div class="col-12">
                                <div class="form-group">
                                        <label for="product_description">Opis produktu</label>
                                        <textarea class="form-control {{ $errors->has('product_description') ? 'is-invalid' : '' }}" required name="product_description" id="product_description" rows="10">{{ old('product_description') }}</textarea>
                                </div>
                        </div>
                    </div>
                    <div class="form-row mt-3">
                        <div class="field is-grouped">
                            <div class="form-group">
                                <label for="product_thumbnail">Dodaj obrazy</label>
                                <input type="file" name="product_thumbnail" id="product_thumbnail">
                            </div>
                        </div>
                    </div>
                    <div class="form-row mt-3">
                        <div class="field is-grouped">
                            <div class="control">
                                <button type="submit" class="btn btn-success">Utwórz projekt</button>
                                <a class="button is-text" href="{{ url('admin/products) }}">
                                    <button type="submit" class="btn btn-danger">Anuluj</button>
                                </a>
                            </div>
                        </div>
                    </div>
                  </form>
<!-- form/end -->

shez1983's avatar

the problem is that you are :

 if($request->hasFile('product_thumbnail')){
            $image = $request->file('product_thumbnail');
            $filename = time().'.'.$image->getClientOriginalName();
            Storage::put($filename, 'images/product_images/');
         }

this stores the file path to somewhere - so you need to then do:

$product->product_thumbnail = "images/product_images/$filename"
$product->save()

the tmp file is the one php creates automatically - which you save somewhere else using storage::put but u then never save that path anywhere..

1 like
ddaniel's avatar

I change all the store function and its seems to be working fine for now.

    public function store(Request $request)  // POST
    {
        // $attributes = request()->validate([
        //     'product_name' => ['required', 'min:3', 'max:60'],
        //     'product_code' => ['nullable', 'numeric'],
        //     'product_price' => ['required', 'min:1', 'max:8'],
        //     'id_subcategory' => ['required'],
        //     'product_thumbnail' => ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg|max:2048'],
        //     'product_short_specification',
        //     'product_description' => ['required']
        // ]);

        // Product::create($attributes);
        
        $request->validate([
            'product_name' => ['required', 'min:3', 'max:60'],
            'product_code' => ['numeric'],
            'product_price' => ['required', 'min:1', 'max:8'],
            'id_subcategory' => ['required'],
            'product_thumbnail' => ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg|max:2048'],
            'product_short_specification',
            'product_description' => ['required']
        ]);

        $product = new Product;
        $product->product_name = $request->input('product_name');
        $product->product_code = $request->input('product_code');
        $product->product_price = $request->input('product_price');
        $product->id_subcategory = $request->input('id_subcategory');
        $product->product_short_specification = $request->input('product_short_specification');
        $product->product_description = $request->input('product_description');

        if($request->hasFile('product_thumbnail')){
            $product->product_thumbnail = $request->input('product_thumbnail');
            $uploadedImage = $request->file('product_thumbnail');
            $originalName  = $request->file('product_thumbnail')->getClientOriginalName();
            $imageName = 'product_thumbnail_' . time() . '_' . $originalName;
            $destinationPath = 'images/product_images/';
            $uploadedImage->move($destinationPath, $imageName);
            $product->product_thumbnail = $destinationPath . $imageName;
         }

        $product->save();
        return redirect('admin/products');
    }
shez1983's avatar

@ddaniel so pretty much what I said... but thats ok dont mark it as correct.. i mean its not as if i didnt spend my time looking at your problem and trying to help you..

Tangente's avatar

Lol !! The response above is....interesting

Please or to participate in this conversation.