app/Http/Controllers/ProductController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $request->validate([
            'code_sku' => 'required',
            'name' => 'required',
            'photo'=> 'image|nullable|max:1999'
        ]);
        if ($request->hasFile('photo')) {
            $filenameWithExt = $request->file('photo')->getClientOriginalName();
            $filename = pathinfo($filenameWithExt,PATHINFO_FILENAME);
            $extension = $request->file('photo')->getClientOriginalExtension();
            $fileNameToStore = $filename.'_'.time().'.'.$extension;
            $path = $request->file('photo')->storeAs('public/photo',$fileNameToStore);
        }
        $product = Product::find($id);
        $product->code_sku = $request->get('code_sku');
        $product->name = $request->get('name');
        if($request->hasfile('photo'))(
            $product->photo = $fileNameToStore);
        $product->save();
        return redirect()->route('product.index')
            ->with('success', 'product Updated Successfully');
    }
public function update ( Request $ request , $ id ) { $ request -> validate ( [ 'code_sku' => 'required' , 'name' => 'required' , 'photo' => 'image|nullable|max:1999' ] ) ; if ( $ request -> hasFile ( 'photo' ) ) { $ filenameWithExt = $ request -> file ( 'photo' ) -> getClientOriginalName ( ) ; $ filename = pathinfo ( $ filenameWithExt , PATHINFO_FILENAME ) ; $ extension = $ request -> file ( 'photo' ) -> getClientOriginalExtension ( ) ; $ fileNameToStore = $ filename . '_' . time ( ) . '.' . $ extension ; $ path = $ request -> file ( 'photo' ) -> storeAs ( 'public/photo' , $ fileNameToStore ) ; } $ product = Product :: find ( $ id ) ; $ product -> code_sku = $ request -> get ( 'code_sku' ) ; $ product -> name = $ request -> get ( 'name' ) ; if ( $ request -> hasfile ( 'photo' ) ) ( $ product -> photo = $ fileNameToStore ) ; $ product -> save ( ) ; return redirect ( ) -> route ( 'product.index' ) -> with ( 'success' , 'product Updated Successfully' ) ; }
Update the specified resource in storage.
Update the specified resource in storage .
// Get filename with the extension // Get just filename // Get just ext // Filename to store // Upload Image