Im trying to update a record in the database, and im using Form Request to validate unique fields, but I cant pass the id to that Form because im just using the route like this:
Route::resource('ganado/engorde', 'GanadoController')->middleware('auth');
In my update controller:
public function update(GanadoEditFormRequest $request, $id)
{
$bovino = Ganado::findOrFail($id);
...
}
This is my form request:
public function rules()
{
return [
'codigo_ganado' => ['required',
Rule::unique('ganado')
->ignore($this->id, 'id_ganado')
->where('lote_actual', $this->lote_actual)
->where('ganaderia', $this->ganaderia)
],
];
}
I tried to pass directly the id = "85" just to check if this "ignore" thing its working and im getting and error too=
id <> 85 instead of id = 85.
How I can ignore the current updating record the right way?
Edit:
This is my "Ganado" table:
1 Answer 1
Since i don't know how your code is look like you might wanna try this :
public function rules()
{
$id = $this->route('id')
// Or
$id = $this->route('engorde')
return [
'codigo_ganado' => ['required',
Rule::unique('ganado')
->ignore($id, 'id_ganado')
->where('lote_actual', $this->lote_actual)
->where('ganaderia', $this->ganaderia)
],
];
}
So base on your Ganado table your primary table is id_ganado, which laravel recognize always tables with id as primary table, so you might need to add
protected $primaryKey = 'id_ganado';
Edit : Update to ignore($id.',id_ganado')
8 Comments
Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from ganado where codigo_ganado = 0604 and id <> 85 and lote_actual = Lote-1 and ganaderia = VS) just to check if im getting the correct id
url.com/ganado/engorde/85to find your item?http://ganaderia.test/ganado/engorde/85/edit@Atlas-Pio