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:
- you using dbms, such
mysql
, notsqlite
- you migrated change database.
Comments
Post a Comment