app/Http/Controllers/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)
    {
        $this->validate($request, [
            'title' => 'required',
            'price' => 'required',
            'image' => 'required',
            'description' => 'required'
        ]);
        $product = new Product();
        if ($request->hasFile('image')) {
            $file = $request->file('image');
            $allowedfileExtension = ['png', 'jpg', 'jpeg'];
            $extension = $file->getClientOriginalExtension();
            $check = in_array($extension, $allowedfileExtension);
            if ($check) {
                $name = $file->getClientOriginalName() . time();
                $file->move('images', $name);
                $product->image = $name;
            }
        }
        $product->title = $request->input('title');
        $product->price = $request->input('price');
        $product->description = $request->input('description');
        $product->save();
        return response()->json([
            'message' =>'Add Product Success',
            'data'    => $product
        ]);
    }
public function store ( Request $ request ) { $ this -> validate ( $ request , [ 'title' => 'required' , 'price' => 'required' , 'image' => 'required' , 'description' => 'required' ] ) ; $ product = new Product ( ) ; if ( $ request -> hasFile ( 'image' ) ) { $ file = $ request -> file ( 'image' ) ; $ allowedfileExtension = [ 'png' , 'jpg' , 'jpeg' ] ; $ extension = $ file -> getClientOriginalExtension ( ) ; $ check = in_array ( $ extension , $ allowedfileExtension ) ; if ( $ check ) { $ name = $ file -> getClientOriginalName ( ) . time ( ) ; $ file -> move ( 'images' , $ name ) ; $ product -> image = $ name ; } } $ product -> title = $ request -> input ( 'title' ) ; $ product -> price = $ request -> input ( 'price' ) ; $ product -> description = $ request -> input ( 'description' ) ; $ product -> save ( ) ; return response ( ) -> json ( [ 'message' => 'Add Product Success' , 'data' => $ product ] ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
# code... # code...