Laravel 5.2 - Use a String as a Custom Primary Key for Eloquent Table becomes 0
This was added to the upgrade documentation on Dec 29, 2015, so if you upgraded before then you probably missed it.
When fetching any attribute from the model it checks if that column should be cast as an integer, string, etc.
By default, for auto-incrementing tables, the ID is assumed to be an integer in this method:
https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model.php#L2790
So the solution is:
class UserVerification extends Model
{
// if your key name is not 'id'
// you can also set this to null if you don't have a primary key
protected $primaryKey = 'your_key_name';
public $incrementing = false;
// In Laravel 6.0+ make sure to also set $keyType
protected $keyType = 'string';
}
Eloquent model returns 0 as primary key
You can try setting public $incrementing = false;
on your model so eloquent doesn't expect your primary key to be an autoincrement primary key.
Laravel 5.2 Eloquent primary key as a multiple key
Eloquent assumes that there is an id column in your table that identifies a row. In your case this column is named blog_id so you need to let Eloquent know.
You can do that by defining primaryKey attribute in your model. It doesn't need to be the primary key in the database as long as it can be used to fetch a single record:
class BlogCounter extends Model {
protected $primaryKey = 'blog_id';
}
Random string primary key for table in Laravel
I would use an integer primary key and add a separate column for the string key (with an unique index). Integer keys are faster and easier to handle (on joins etc.).
If the string key has a fixed length, you should use a CHAR
column to get the best performance.
I would put the key generation into the controller:
$key = str_random(6);
while(YourModel::where('key', $key)->exists()) {
$key = str_random(6);
}
$yourModel->key = $key;
Or you just try a key and catch the unlikely case of a duplicate value:
$yourModel->key = str_random(6);
try {
$yourModel->save();
catch(Illuminate\Database\QueryException $e) {
<code from above>
$yourModel->save();
}
Obtaining field from table using primary key
You can access the model that given revision relates to by accessing the revisionable relation of that revision. In your case, in order to display the email property of related model, you could do the following:
<td>{{ $revision->revisionable->email }}</td>
Related Topics
Dynamically Create PHP Object Based on String
Resize/Crop/Pad a Picture to a Fixed Size
PHP Get Highest Value from Array
Setcookie, Cannot Modify Header Information - Headers Already Sent
PHP Header() Redirect with Post Variables
How to Write JavaScript Code Inside PHP
PHP Unserialize Fails with Non-Encoded Characters
How to Deep Copy a Datetime Object
Is There Garbage Collection in PHP
How to Send Cookies Using PHP Curl in Addition to Curlopt_Cookiefile
Increase Max_Execution_Time in PHP
Extract Text from Doc and Docx
Returning a Value in Constructor Function of a Class
How to Make a String in PHP with a Backslash in It