Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How To Set Custom Primary Key for CRUD #179

Closed
ahmadronaghdev opened this issue Oct 13, 2016 · 7 comments
Closed

How To Set Custom Primary Key for CRUD #179

ahmadronaghdev opened this issue Oct 13, 2016 · 7 comments

Comments

@ahmadronaghdev
Copy link

class UserPayment extends Model
{
use CrudTrait;

/*
GLOBAL VARIABLES
--------------------------------------------------------------------------
*/
protected $table = 'user_payment';
protected $primaryKey = 'token';
public $timestamps = true;
protected $guarded = ['token'];

.....
}

I Create Model Like This But When need Edit Curd

The Link is generated like this:
http://localhost:8000/adminpanel/user-payment//edit

primary key is token not id

how can i fix this problem?

@tabacitu
Copy link
Member

Hmm. This has been fixed a long time ago. What version of Laravel and Backpack\CRUD are you using?

@tabacitu
Copy link
Member

A composer update might fix it.

@ahmadronaghdev
Copy link
Author

I use laravel 5.3 and Backpack v 3.1.17

@ahmadronaghdev
Copy link
Author

ahmadronaghdev commented Oct 13, 2016

I Update Laravel and Backpack\CRUD to last version its not work for me
The token is string primary key and dosen't work for me,i think work with integer value
the edit button link is like below:

token is : "Gp8CyJ015_FL4J3C"
edit link : http://localhost:8000/adminpanel/user-payment/0/edit

token is: "83869054625463495"
edit link :http://localhost:8000/adminpanel/user-payment/83869054625463495/edit

@tabacitu
Copy link
Member

Oh. You should't use a string as primary key. It's bad practice. The search is much much slower so it defeats the purpose.

@ahmadronaghdev
Copy link
Author

tnx i will change database primary key to id

@tabacitu
Copy link
Member

tabacitu commented Apr 25, 2019

Later update: it IS possible to use strings as primary keys. Just make sure you define public $incrementing = false; on your model, and the result to $entry->getKey() will be the correct one.

This was referenced Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants