app/Http/Controllers/ProductController.php
/**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return JsonResponse
     */
Store a newly created resource in storage.
@param Request $request
@return JsonResponse
Store a newly created resource in storage . @param Request $request @return JsonResponse
public function store(Request $request)
    {
        $request->validate(
            [
                'category_id' => 'required|exists:categories,id',
                'sub_category_id' => 'required|exists:sub_categories,id',
                'brand_id' => 'nullable|exists:brands,id',
                'name' => 'required|max:255|unique:products,name',
                'description' => 'required',
                'file' => 'required|max:255',
            ]
        );
        if ($request->hasFile('file'))
        {
            $image = Helper::save_file($request->file, 'products');
            $request['image'] = $image;
        }
        $product_code = $this->generateProductCode();
        $request['product_code'] = $product_code;
        $data = collect($request);
        if ($request->hasFile('file')) {
            $data->forget('file');
        };
        $product = Product::query()->create($data->toArray());
        if ($product)
        {
            return Helper::response_with_data($product,false);
        }
        return Helper::response_with_data(null, true);
    }
public function store ( Request $ request ) { $ request -> validate ( [ 'category_id' => 'required|exists:categories,id' , 'sub_category_id' => 'required|exists:sub_categories,id' , 'brand_id' => 'nullable|exists:brands,id' , 'name' => 'required|max:255|unique:products,name' , 'description' => 'required' , 'file' => 'required|max:255' , ] ) ; if ( $ request -> hasFile ( 'file' ) ) { $ image = Helper :: save_file ( $ request -> file , 'products' ) ; $ request [ 'image' ] = $ image ; } $ product_code = $ this -> generateProductCode ( ) ; $ request [ 'product_code' ] = $ product_code ; $ data = collect ( $ request ) ; if ( $ request -> hasFile ( 'file' ) ) { $ data -> forget ( 'file' ) ; } ; $ product = Product :: query ( ) -> create ( $ data -> toArray ( ) ) ; if ( $ product ) { return Helper :: response_with_data ( $ product , false ) ; } return Helper :: response_with_data ( null , true ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .