app/Http/Controllers/Admin/ProductController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $data = $request->all();
        DB::transaction(function() use ($id, $data, $request) {
            $product = $this->product->find($id);
            if($request->hasFile('image')) {
                $oldImages = $this->productImage->where('product_id', $id)->get();
                $images = $request->file('image');
                foreach ($images as $key => $image) {
                    if(!strpos($oldImages[$key]->path, 'http')) {
                        Storage::delete(str_replace('storage', 'public', $oldImages[$key]->path));
                    }
                    $filename = str_random(28) . '.jpg';
                    $path = 'public/products/' . $filename;
                    $file = Image::make($image->getRealPath())->encode('jpg',75);
                    $size = $file->filesize();
                    Storage::put($path, (string) $file);
                    $imageUrl = Storage::url($path);
                    $this->productImage->find($oldImages[$key]->id)->update([
                        'path' => $imageUrl,
                        'thumbnail' => $imageUrl,
                        'size' => $size
                    ]);
                }
            }
            $product->update($data);
        }, 3);
        return redirect()->route('admin.product.show', [$id]);
    }
public function update ( Request $ request , $ id ) { $ data = $ request -> all ( ) ; DB :: transaction ( function ( ) use ( $ id , $ data , $ request ) { $ product = $ this -> product -> find ( $ id ) ; if ( $ request -> hasFile ( 'image' ) ) { $ oldImages = $ this -> productImage -> where ( 'product_id' , $ id ) -> get ( ) ; $ images = $ request -> file ( 'image' ) ; foreach ( $ images as $ key => $ image ) { if ( ! strpos ( $ oldImages [ $ key ] -> path , 'http' ) ) { Storage :: delete ( str_replace ( 'storage' , 'public' , $ oldImages [ $ key ] -> path ) ) ; } $ filename = str_random ( 28 ) . '.jpg' ; $ path = 'public/products/' . $ filename ; $ file = Image :: make ( $ image -> getRealPath ( ) ) -> encode ( 'jpg' , 75 ) ; $ size = $ file -> filesize ( ) ; Storage :: put ( $ path , ( string ) $ file ) ; $ imageUrl = Storage :: url ( $ path ) ; $ this -> productImage -> find ( $ oldImages [ $ key ] -> id ) -> update ( [ 'path' => $ imageUrl , 'thumbnail' => $ imageUrl , 'size' => $ size ] ) ; } } $ product -> update ( $ data ) ; } , 3 ) ; return redirect ( ) -> route ( 'admin.product.show' , [ $ id ] ) ; }
Update the specified resource in storage.
Update the specified resource in storage .