Ali
Ali

Reputation: 669

SoftDelete doesn't work when I change primary key in Laravel

I have changed the primary key in a table from id to pr_id and mobile.


$table->primary(['pr_id' , 'mobile']);

also I added SoftDelete Trait in model. but when I want to delete a record it doesn't work.

Upvotes: 5

Views: 372

Answers (2)

Kamal Paliwal
Kamal Paliwal

Reputation: 1301

You need to override few methods also like getKeyForSaveQuery, setKeysForSaveQuery with defining the primary key in model. For soft delete you need to override one more method runSoftDelete.

Reference links

Upvotes: 1

Ayaz Ali Shah
Ayaz Ali Shah

Reputation: 3541

I believe this is because of not mentioning primary key into model, and your model still consider primary key is id however you have changed it. So you just to add following script into relevant model,

class YourModelClass extends Model
{
     protected $primaryKey = 'pr_id';
}

This in way model won't consider primary key as id.

Upvotes: 3

Related Questions