0

INITIAL NOTE: I AM WILL AWARE OF HOW TO IGNORE ROWS BY PRIMARY KEY! THAT IS NOT THE ISSUE AT HAND!

First, let me give you the structure of my database table...

+-------------+------------+------+-----+---------------------+-----------------------------+
| Field       | Type       | Null | Key | Default             | Extra                       |
+-------------+------------+------+-----+---------------------+-----------------------------+
| id          | int(11)    | NO   | PRI | NULL                | auto_increment              |
| title       | text       | NO   |     | NULL                |                             |
| description | text       | NO   |     | NULL                |                             |
| created_at  | timestamp  | NO   |     | 0000-00-00 00:00:00 | on update CURRENT_TIMESTAMP |
| updated_at  | timestamp  | NO   |     | 0000-00-00 00:00:00 |                             |
| created_by  | int(11)    | NO   |     | NULL                |                             |
| public      | tinyint(1) | NO   |     | 1                   |                             |
+-------------+------------+------+-----+---------------------+-----------------------------+

I want it so that if I am storing a new topic(row) in the database, it will only check rows where public = 1 so if a post is "deleted"(or public = 0), it will not count in the unique verification.

This is how I currently handle NEW posts:

'title' => 'required|unique:topics',

This is how I currently handle UPDATING posts:

'title' => 'required|unique:topics,title,'.Request::input('id'),

Any help is appricatied. Thank you!

Robert Trainor
  • 39
  • 1
  • 10

1 Answers1

1

Based on the Validation documentation at Laravel:
unique:table,column,except,idColumn

Also, from: http://brianretterer.com/quick-tip-laravel-unique-validation/

'title' => 'required|unique:articles,title,NULL,NULL,public,0'

PaulELI
  • 444
  • 6
  • 16
  • This did not solve my issue. It still flags the "deleted" post and doesn't let the new topic be stored. – Robert Trainor Sep 24 '15 at 00:11
  • I am not sure what you are trying to accomplish then... Can you clarify the functionality you wish to achieve? – PaulELI Sep 24 '15 at 01:54
  • Ok, so I want the title of the new post to be one that no other PUBLIC (public=1) post has. If the post has public=0, I want that row to be ignored – Robert Trainor Sep 24 '15 at 01:55
  • I edited my post, let me know if that works for you. – PaulELI Sep 24 '15 at 01:58
  • Okay, let me fire up a box and see what I can do for you. Give me a few. – PaulELI Sep 24 '15 at 02:01
  • Okay, try the new edit: `'title' => 'required|unique:articles,title,NULL,NULL,public,0'` – PaulELI Sep 24 '15 at 02:19
  • Weird, worked for me with the same setup you have. Are you sure you set up your database properly? Didn't set the title field to unique? – PaulELI Sep 24 '15 at 03:04
  • `'title' => 'required|unique:topics,title,NULL,NULL,public,0',` That's the exact line in my request class. It absolutely refuses to work. Just keeps telling me it is already in use – Robert Trainor Sep 24 '15 at 03:07
  • http://stackoverflow.com/questions/19377069/laravel-4-validation-unique-database-multiple-where-clauses Has something similar to this issue. – PaulELI Sep 24 '15 at 03:15
  • 1
    Okay, so the code you gave me got me 99.9% of the way. The only issue was the I misunderstood how the query works and I changed `public,0` to `public,1`... that solved it. Thank you for your help! – Robert Trainor Sep 24 '15 at 03:20