Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4: making a combination of values/columns unique

I'm importing a bunch of csv entries in my database with Laravel 4.

I can't really point at one column that has to be unique, it's a combination of 5 columns that makes it unique. However: how does one define this in Laravel?

Option 1: schema builder
You can use the $table->unique('email') method, but that only seems to allow one column, not a combination of columns.

Option 2: Validation
Less preferable, but I could validate the model before inserting it. However, again, using 'unique:[table]' validation rules, it will return an error when just one of the column values isn't unique, not a combination of them.

Can anyone tell me how I should go about this?
I'm sure I'm missing something, but I could use a push in the right direction :-)

Thanks,

Dieter

like image 740
Dieter Avatar asked Jun 07 '13 18:06

Dieter


People also ask

How do you make a unique two column combination in laravel?

2 Answers. Show activity on this post. $table->unique(['volume','number']); By having this constraint set, if you insert 'Volume 1 , Number 1` once it'll be fine.

How do you make a unique two column combination?

To define a UNIQUE on multiple columns, we put a comma-separated columns list inside parenthesis that follows the UNIQUE keyword.


1 Answers

You can combine:

$table->unique( array('email','name') ); 

And pretty much everything in Laravel will accept arrays to do whatever you need to with 'more than one'.

like image 88
Antonio Carlos Ribeiro Avatar answered Sep 18 '22 20:09

Antonio Carlos Ribeiro