app/Http/Controllers/Admin/ProductController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $data = $request->all();
        $product = $this->product;
        DB::transaction(function () use ($data, $request, $product) {
            $product = $product->create($data);
            if($request->hasFile('image')) {
                $images = $request->file('image');
                foreach ($images as $key => $image) {
                    $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->create([
                        'product_id' => $product->id,
                        'path' => $imageUrl,
                        'thumbnail' => $imageUrl,
                        'size' => $size
                    ]);
                }
            }
        }, 3);
        return redirect()->route('admin.product.show', [$product->id]);
    }
public function store ( Request $ request ) { $ data = $ request -> all ( ) ; $ product = $ this -> product ; DB :: transaction ( function ( ) use ( $ data , $ request , $ product ) { $ product = $ product -> create ( $ data ) ; if ( $ request -> hasFile ( 'image' ) ) { $ images = $ request -> file ( 'image' ) ; foreach ( $ images as $ key => $ image ) { $ 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 -> create ( [ 'product_id' => $ product -> id , 'path' => $ imageUrl , 'thumbnail' => $ imageUrl , 'size' => $ size ] ) ; } } } , 3 ) ; return redirect ( ) -> route ( 'admin.product.show' , [ $ product -> id ] ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .