Reputation: 1101
I have this table and this is my migration for the same
$table->increments('id');
$table->integer('voucher_id');
$table->integer('voucher_item_id');
$table->integer('voucher_item_quantity');
$table->integer('voucher_dealer');
$table->integer('voucher_item_main_category');
$table->integer('voucher_item_sub_category');
$table->integer('voucher_type');
$table->timestamps();
$table->softDeletes();
and this is the model
use softDeletes;
use Notifiable;
protected $primaryKey = 'voucher_id';
protected $table = 'vouchers';
now the voucher_id is the primary key
..
and its can be the same number for multi rows ..
like this
id voucher_id item_total
1 1 15
2 1 16
3 1 17
now in the page vouchers.index
its going to show the three rows id,1 id,2 id,3
how can I let Laravel remove the duplication from the primary key and use only one number from the duplication
and how can I make one-to-many
relationship in one table
like
select * from vouchers where voucher_id=1
Upvotes: 0
Views: 1062
Reputation: 2047
To make a column unique:
$table->integer('voucher_id')->unique();`
But if you only need the voucher_id
as the primary key to that table i would recommend you to remove the id
column and add the following:
$table->integer('voucher_id');
$table->primary('voucher_id');
This way you will have voucher_id
as primary key and it will be an unique value.
You migration will be:
$table->integer('voucher_id');
$table->primary('voucher_id');
$table->integer('voucher_item_id');
$table->integer('voucher_item_quantity');
$table->integer('voucher_dealer');
$table->integer('voucher_item_main_category');
$table->integer('voucher_item_sub_category');
$table->integer('voucher_type');
$table->timestamps();
$table->softDeletes();
Upvotes: 1
Reputation: 9455
Change your migration like this:
$table->integer('voucher_id');
$table->primary('voucher_id');
$table->integer('voucher_item_id');
$table->integer('voucher_item_quantity');
$table->integer('voucher_dealer');
$table->integer('voucher_item_main_category');
$table->integer('voucher_item_sub_category');
$table->integer('voucher_type');
$table->timestamps();
$table->softDeletes();
Upvotes: 1