Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

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

question from:https://stackoverflow.com/questions/16990723/laravel-4-making-a-combination-of-values-columns-unique

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...