0

Im trying to upload an image in my Laravel app. but have an error:

array_key_exists(): The first argument should be either a string or an integer

Im using the enctype: <form method="POST" action="/producto" enctype="multipart/form-data">

This is my controller:

public function CrearProducto(Request $data)
{
    $data->validate(
        [
            'nombre_producto'=>'required | min:1 | max:250',
            'precio_producto'=>'required | min:1 | max:250',
            'stock_producto'=>'required | min:1 | max:250',
            'igv_producto'=>'required | min:1 | max:250',
            'producto_categoria'=>'required | min:1 | max:250',
            'imagen_producto'=>'mimes:jpeg,bmp,png,jpg',
        ]

    );

    
        $producto = new Producto();
        $producto->nombre_producto = $data["nombre_producto"];
        $producto->precio_producto = $data["precio_producto"];
        $producto->stock_producto = $data["stock_producto"];
        $producto->igv_producto = $data["igv_producto"];
        $producto->producto_categoria = $data["producto_categoria"];
        if($data["imagen_producto"==""]){
            $producto->imagen_producto = $data["imagen_producto"];
        }else{
            $imagen = $data["imagen_producto"];
            $nombre_img_prod = time()."_".$imagen->getClientOriginalName();
            \Storage::disk('public')->put($nombre_img_prod,  \File::get($imagen));

            $producto->imagen_producto = $nombre_img_prod;
            
        }
        //$producto->imagen_producto = "-";
        $producto->tipo_afectacion_producto = "-";
        $producto->estado_producto = "Activo";
        $producto->save();

        return redirect()->route('producto');
    
}
1
  • 2
    Along with that message should be a stacktrace showing the specific line that is throwing, along with how it got there. Commented May 13, 2022 at 4:07

1 Answer 1

1

Validation rules cannot have spaces. Try to simplify the method and use everything that the Laravel framework offers you.

Example:


    public function crearProducto(Request $request)
    {
        // Validation rules cannot have spaces.
        $request->validate([
                'nombre_producto'    => 'required|min:1|max:250',
                'precio_producto'    => 'required|min:1|max:250',
                'stock_producto'     => 'required|min:1|max:250',
                'igv_producto'       => 'required|min:1|max:250',
                'producto_categoria' => 'required|min:1|max:250',
                'imagen_producto'    => 'nullable|image',
            ]
        );

        $producto = new Producto;

        $producto->nombre_producto    = $request->nombre_producto;
        $producto->precio_producto    = $request->precio_producto;
        $producto->stock_producto     = $request->stock_producto;
        $producto->igv_producto       = $request->igv_producto;
        $producto->producto_categoria = $request->producto_categoria;

        if ($request->hasFile('imagen_producto')) {
            $producto->imagen_producto = $request
                ->file('imagen_producto')
                ->store('images', 'public');
        }
        
        //$producto->imagen_producto = "-";
        $producto->tipo_afectacion_producto = "-";
        $producto->estado_producto          = "Activo";
        $producto->save();

        return to_route('producto');
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.