2

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:

enter image description here

asked Aug 13, 2020 at 21:59
2
  • did u used something like this url.com/ganado/engorde/85 to find your item? Commented Aug 13, 2020 at 22:02
  • yeah my update controller its working fine, this is an example: http://ganaderia.test/ganado/engorde/85/edit @Atlas-Pio Commented Aug 13, 2020 at 22:05

1 Answer 1

3

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')

answered Aug 13, 2020 at 22:14
Sign up to request clarification or add additional context in comments.

8 Comments

ok the second one its working and im getting the id, but still getting the unique validation message, just to check if im getting the id I removed the 'id_ganado' to get sql error that id column its unknown and here I can check that im getting the id, why still isnt working to ignore current record?
Here is the error that im saying: 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
yeah thats why i asked that later in the question, because I tried passing directly the id just to check if it was working and it was not working. Already updated the question with the table.
Already have defined the primary key like that way in my model @Atlas-Pio
mmm you mean with the -> after the ignore word? because thats a wrong way to put it
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.