app/Http/Controllers/ProductController.php
/**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param Product $product
     * @return JsonResponse
     */
Update the specified resource in storage.
@param Request $request
@param Product $product
@return JsonResponse
Update the specified resource in storage . @param Request $request @param Product $product @return JsonResponse
public function update(Request $request, Product $product)
    {
        $request->validate(
            [
                'category_id' => 'nullable|exists:categories,id',
                'sub_category_id' => 'nullable|exists:sub_categories,id',
                'unit_id' => 'nullable|exists:units,id',
                'brand_id' => 'nullable|exists:units,id',
                'name' => 'nullable|max:255|unique:products,name',
                'description' => 'nullable',
                'image' => 'nullable|max:255',
            ]
        );
        if ($request->hasFile('file'))
        {
            $image = Helper::save_file($request->file, 'products');
            $request['image'] = $image;
        }
        $data = collect($request);
        if ($request->hasFile('file')) {
            $data->forget('file');
        };
        if ($product->update($data->toArray()))
        {
            return Helper::response_with_data(Product::query()->find($product->id),false);
        }
        return Helper::response_with_data(null, true);
    }
public function update ( Request $ request , Product $ product ) { $ request -> validate ( [ 'category_id' => 'nullable|exists:categories,id' , 'sub_category_id' => 'nullable|exists:sub_categories,id' , 'unit_id' => 'nullable|exists:units,id' , 'brand_id' => 'nullable|exists:units,id' , 'name' => 'nullable|max:255|unique:products,name' , 'description' => 'nullable' , 'image' => 'nullable|max:255' , ] ) ; if ( $ request -> hasFile ( 'file' ) ) { $ image = Helper :: save_file ( $ request -> file , 'products' ) ; $ request [ 'image' ] = $ image ; } $ data = collect ( $ request ) ; if ( $ request -> hasFile ( 'file' ) ) { $ data -> forget ( 'file' ) ; } ; if ( $ product -> update ( $ data -> toArray ( ) ) ) { return Helper :: response_with_data ( Product :: query ( ) -> find ( $ product -> id ) , false ) ; } return Helper :: response_with_data ( null , true ) ; }
Update the specified resource in storage.
Update the specified resource in storage .