app/Http/Controllers/Admin/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)
    {
        $this->validate($request,
            [
            'title'=>'bail|required|max:255',
            'price'=>'required',
            'description'=>'required|max:100'
            ,
            'imagePath'=>'required'
            ],[
            'required'=>'El campo :attribute es obligatorio'
            ]
            );
        $product=Product::create([
            'title'=>$request->input('title'),
            'price'=>$request->input('price'),
            'description'=>$request->input('description'),
            'imagePath'=>str_slug($request->input('imagePath')), 
            'categorie_id'=>$request->input('category')
            ]);
        $nombreImagen = $product->id . '.' . str_slug($product->title) . '.' .request()->imagePath->extension();
        request()->imagePath->storeAs('public', $nombreImagen);
        $product->imagePath = $nombreImagen;
        $product->save();
        return redirect()->action('Admin\ProductsController@index');
    }
public function store ( Request $ request ) { $ this -> validate ( $ request , [ 'title' => 'bail|required|max:255' , 'price' => 'required' , 'description' => 'required|max:100' , 'imagePath' => 'required' ] , [ 'required' => 'El campo :attribute es obligatorio' ] ) ; $ product = Product :: create ( [ 'title' => $ request -> input ( 'title' ) , 'price' => $ request -> input ( 'price' ) , 'description' => $ request -> input ( 'description' ) , 'imagePath' => str_slug ( $ request -> input ( 'imagePath' ) ) , 'categorie_id' => $ request -> input ( 'category' ) ] ) ; $ nombreImagen = $ product -> id . '.' . str_slug ( $ product -> title ) . '.' . request ( ) -> imagePath -> extension ( ) ; request ( ) -> imagePath -> storeAs ( 'public' , $ nombreImagen ) ; $ product -> imagePath = $ nombreImagen ; $ product -> save ( ) ; return redirect ( ) -> action ( 'Admin\ProductsController@index' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// guardar la imagen //str slug sanitiza el nombre y los espacios los pone con un guion // storeAs recibe dos parametros el nombre de la carpeta y el nombre del archivo