laravel - How to make combination of two columns as a unique key? -


i have table :

public function up() {     schema::create('edition', function (blueprint $table) {         $table->increments('id');         $table->integer('volume');         $table->text('cover')->nullable();         $table->enum('number', ['1', '2']);         $table->timestamps();     });     schema::table('journal', function(blueprint $table) {         $table->foreign('id_edition')->references('id')->on('edition')->ondelete('cascade')->onupdate('cascade');     }); } 

i wanted make combination of column volume , number unique key. example, there data "volume 1, number 1" , when user insert same combination throw error message data exist. there way can in laravel ?

just include following snippet inside up() method

$table->unique(['volume','number']); 

by having constraint set, if insert 'volume 1 , number 1` once it'll fine. however, if try same again, it'll throw error.

conditions:

  1. you using dbms, such mysql , not sqlite
  2. you migrated change database.

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -