Aux1234
Aux1234

Reputation: 53

How to pass id to Form Request in Laravel

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

Upvotes: 2

Views: 2011

Answers (1)

Atlas-Pio
Atlas-Pio

Reputation: 1153

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

Upvotes: 3

Related Questions