app/Http/Controllers/Panel/ProductsController.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 = $this->validate($request, [
            'title' => 'required|unique:products,title',
            'category_id' => 'required|exists:categories,id',
            'subcategory_id' => ['nullable','exists:subcategories,id', new Rules\InRelation('subcategories', 'category_id')],
            'real_price' => 'required|numeric|min:0',
            'offer_price' => 'nullable|numeric|min:0',
            'offer_percent' => 'nullable|numeric|min:0',
            'shipping_price' => 'nullable|numeric|min:0',
            'shipping_days' => 'nullable|numeric|min:1',
            'description' => 'required',
        ],[
            'required' => 'Este campo es requerido.',
            'category_id.exists' => 'La categoría seleccinada es inválida.',
            'subcategory_id.exists' => 'La sub-categoría seleccinada es inválida.',
            'numeric' => 'Este valor debe ser numérico.',
            'min' => 'Este valor no puede ser menor a :min.'
        ]);
        $product = new Product;
        $tmp = $product->checkFileTmp($request->get('hash'));
        $product->title = $data['title'];
        $product->category_id = $data['category_id'];
        $product->subcategory_id = $data['subcategory_id'];
        $product->real_price = $data['real_price'];
        $product->offer_price = percentOfValue($data['real_price'], $data['offer_percent']);
        $product->offer_percent = $data['offer_percent'];
        $product->shipping_price = $data['shipping_price'];
        $product->shipping_days = $data['shipping_days'];
        $product->description = $data['description'];
        $product->save();
        $product->uploadRealFiles($product->id, $tmp);
        return [
            'success' => true,
            'message' => 'Guardado exitosamente',
            'url' => route('products.index')
        ];
    }
public function store ( Request $ request ) { $ data = $ this -> validate ( $ request , [ 'title' => 'required|unique:products,title' , 'category_id' => 'required|exists:categories,id' , 'subcategory_id' => [ 'nullable' , 'exists:subcategories,id' , new Rules \ InRelation ( 'subcategories' , 'category_id' ) ] , 'real_price' => 'required|numeric|min:0' , 'offer_price' => 'nullable|numeric|min:0' , 'offer_percent' => 'nullable|numeric|min:0' , 'shipping_price' => 'nullable|numeric|min:0' , 'shipping_days' => 'nullable|numeric|min:1' , 'description' => 'required' , ] , [ 'required' => 'Este campo es requerido.' , 'category_id.exists' => 'La categoría seleccinada es inválida.', 'subcategory_id.exists' => 'La sub-categoría seleccinada es inválida.', 'numeric' => 'Este valor debe ser numérico.', 'min' => 'Este valor no puede ser menor a :min.' ] ) ; $ product = new Product ; $ tmp = $ product -> checkFileTmp ( $ request -> get ( 'hash' ) ) ; $ product -> title = $ data [ 'title' ] ; $ product -> category_id = $ data [ 'category_id' ] ; $ product -> subcategory_id = $ data [ 'subcategory_id' ] ; $ product -> real_price = $ data [ 'real_price' ] ; $ product -> offer_price = percentOfValue ( $ data [ 'real_price' ] , $ data [ 'offer_percent' ] ) ; $ product -> offer_percent = $ data [ 'offer_percent' ] ; $ product -> shipping_price = $ data [ 'shipping_price' ] ; $ product -> shipping_days = $ data [ 'shipping_days' ] ; $ product -> description = $ data [ 'description' ] ; $ product -> save ( ) ; $ product -> uploadRealFiles ( $ product -> id , $ tmp ) ; return [ 'success' => true , 'message' => 'Guardado exitosamente' , 'url' => route ( 'products.index' ) ] ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
# validaciones # Modelo de los productos # Verificar directorio temporal # Datos a guardar # Guardar # Subir archivos # Respuesta