1

I am trying to limit the required:unique validator in my Laravel 5.2 app to checking uniqueness of my 'slug' column against only the table results of a specific user_id. In other words, the slug does need to be unique, but only on a per-user basis. I've got it working for the store() function, with the following code:

'slug' => 'required|unique:contents,slug,NULL,id,user_id,' . Auth::id()

That works just fine. However, I can't get the update() function to work the same, because I can't get the validator to stop checking unique against the result of the entry that's currently being updated. The code I'm currently using is:

'slug' => 'required|unique:contents,slug,' . $content->slug . ',id,user_id,' . Auth::id()

That code doesn't look right to me, but I've tried several variants and none of them work how I would like. To my mind, I would think that this would work:

'slug' => 'required|unique:contents,id,' . $content->id . ',user_id,' . Auth::id()

But it doesn't. Does anybody know what my problem is here? Any guidance would be much appreciated.

Luc Boone
  • 75
  • 1
  • 5
  • In the second bit of code, are you aware that you have `$content->slug`, rather than `$content->id`? Other than that minor error, I would expect that second code to work – Jonathon Aug 18 '16 at 19:33
  • Yeah I didn't think the second line looked right either, the third line, which is: 'slug' => 'required|unique:contents,id,' . $content->id . ',id,user_id,' . Auth::id() is the one that I expect to work, but it doesn't. It allows me to update the slug to one that already exists for that user. – Luc Boone Aug 18 '16 at 19:39
  • The last line of code is missing `,id`, perhaps that's why it's not working. Try: `'required|unique:contents,id,' . $content->id . ',id,user_id,' . Auth::id()` – Jonathon Aug 18 '16 at 19:58
  • My bad, Jonathon, I did make a typo in the third line in my question. The correct version was in my previous comment, though. The correct answer was from Jody, below. Thanks for your help! – Luc Boone Aug 18 '16 at 21:19

1 Answers1

7

It's hard to give the right answer without knowing the actual attribute names in your model. The validator supports the following parameters

unique:{0},{1},{2},{3},{4},{5}

where:

{0} = table name being validated

{1} = column name being validated

{2} = Id value of row in {0} to skip validation

{3} = name of primary key column in {0}

{4} = name of additional filter column in {0}

{5} = value for additional filter column

So, it seems we clearly have the values for {0}, {1}, {2} and {5}. I am not sure from your question information what the correct values for {3} and {4} are. If I was to take a guess, I would write the validator rule like this:

'slug' => 'required|unique:contents,slug,' . $content->id . ',id,user_id,' . Auth::id()
Jody Boucher
  • 331
  • 1
  • 7
  • Thank you so much for the validator param definitions. I hadn't seen those anywhere, not even on the Laravel site. I get it now. Thanks again! – Luc Boone Aug 18 '16 at 21:20