Robert Trainor
Robert Trainor

Reputation: 39

Laravel Validation Unique -- Ignore condition

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!

Upvotes: 0

Views: 3174

Answers (1)

PaulELI
PaulELI

Reputation: 444

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'

Upvotes: 1

Related Questions