Reputation: 501
I'm trying to save/update data with this method save
, but it doesnt work, it says that a parameter doesn't have a default value, but I'm sending it as a part of what I want to insert/update. Here is my code
public function store(Request $request)
{
$producto_nombre = Producto_nombre::where('codigo', $request->codigo)->first();
$reglas = [];
//Comprobar unique en update
if (isset($producto_nombre)) {
$reglas = [
'codigo' => [
'required',
Rule::unique('producto_nombre')->ignore($producto_nombre->id),
],
'descripcion' => 'required'];
} else {
$reglas = ['descripcion' => 'required', 'codigo' => 'required|unique:producto_nombre'];
}
$validator = Validator::make($request->all(), $reglas);
if ($validator->fails()) {
return response()->json(array('errors' => $validator->getMessageBag()->toArray()));
} else {
$collection = $request->all();
$this->agregarProducto($collection);
}
}
public function agregarProducto($collection)
{
$producto_nombre = Producto_nombre::where('codigo',
$collection['codigo'])->first();
$empresa = Empresa::where('id', $collection['empresa_id'])->first();
//Si esta vacio crea una nuevo
if (empty($producto_nombre)) {
$producto_nombre = new Producto_nombre();
}
$collection['iva_id'] = Iva::where('actual', 1)->first()->id;
// dd($collection);
// $producto_nombre->iva_id = Iva::where('actual', 1)->first()->id;
$producto_nombre->save($collection);
$producto = $empresa->productos()->where('producto_nombre_id',
$producto_nombre->id)->first();
// Si esta vacio crea nuevo producto
if (empty($producto)) {
$producto = new Producto();
$this->crearProductoSucursales($producto_nombre->id,
$collection['empresa_id']);
}
$producto->producto_nombre_id = $producto_nombre->id;
// TODO editar en transferencias y compras en 0 maximo y minimo
$producto->stock_maximo = $collection['stock_maximo'];
$producto->stock_minimo = $collection['stock_minimo'];
$producto->descuento = $collection['descuento'];
$producto->save();
$empresa->productos()->syncWithoutDetaching($producto->id);
}
This is what $collection has
array:13 [
"codigo" => "prueba"
"descripcion" => "prueba"
"con_iva" => "1"
"desglose" => "0"
"costo_actual" => "12"
"precio" => "19.64"
"descuento" => "12"
"costo_promedio" => "0"
"maximo" => "45"
"minimo" => "2"
"linea" => "2"
"seccion" => "2"
"empresa_id" => "2"
]
I've created the method agregarProducto
because I want to reuse it in other controllers
Error Says:
"message": "SQLSTATE[HY000]: General error: 1364 Field 'codigo' doesn't have a default value (SQL: insert into producto_nombre
(updated_at, created_at) values (2018-04-05 10:09:28, 2018-04-05 10:09:28))",
Upvotes: 0
Views: 5198
Reputation: 15131
Go to class Producto_nombre
and add:
class Producto_nombre
{
...
protected $fillable = ['codigo'];
...
Then replace
$producto_nombre->save($collection);
with
$producto_nombre->fill($collection);
$producto_nombre->save();
The parameters for method save()
will work for things like disable timestamp and other options. In your case, you are trying to fill the object with data from array, so you must use fill($collection)
or, if it's a new register, you could use create($collection)
Upvotes: 4