php - Laravel 5.1 - Update name for current entry, must be unique to current User ID -
i've got code below in geckorequest
file. works part, if try update entry , keep name fails saying name needs unique... no other entries have name apart current entry loaded in edit form.
what best thing me check name unique current user allow me update same entry without changing name?
public function rules() { switch($this->method()) { case 'get': case 'delete': { return []; } case 'post': { return [ 'morph' => 'required', 'sex' => 'required', 'genetics' => 'required', 'name' => "required|unique:geckos,name,null,id,user_id," . \auth::user()->id ]; } case 'put': case 'patch': { return [ 'morph' => 'required', 'sex' => 'required', 'genetics' => 'required', 'name' => "required|unique:geckos,name,null,id,user_id," . \auth::user()->id ]; } default:break; } }
you should able specify id of gecko resource in rule.
case 'patch': { //this assuming you're using route model binding //and it's called gecko , not geckos (or else). $id = $this->route('gecko')->id; return [ 'morph' => 'required', 'sex' => 'required', 'genetics' => 'required', 'name' => "required|unique:geckos,name,{$id},id,user_id," . \auth::user()->id ]; }
this tell laravel has unique unless ids match.
nb when using route method in form validation class looking additional params have been passed through in url (these parts of url wrapped {}
in routes file). depending on how these have been set and/or version of laravel
you're using possible laravel have resolved param model.
if unsure how laravel storing param in route, in rules()
method add dd($this->route());
, submit form , @ response. value in parameters
array.
if not being stored model , it's literally id: $id = $this->route('gecko')
.
hope helps!
Comments
Post a Comment