Laravel 5.2 - Use a String as a Custom Primary Key for Eloquent Table becomes 0

PhpMysqlLaravelLaravel 5Eloquent

Php Problem Overview


I am trying to use email as my table's primary key, so my eloquent code is-

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserVerification extends Model
{
	protected $table = 'user_verification';
    protected $fillable = 	[
						        'email',
						        'verification_token'
						    ];
	//$timestamps = false;
	protected $primaryKey = 'verification_token';
}

And my DB is like this-

enter image description here

but if I do this-

UserVerification::where('verification_token', $token)->first();

I am getting this-

{
  "email": "[email protected]",
  "verification_token": 0,
  "created_at": "2016-01-03 22:27:44",
  "updated_at": "2016-01-03 22:27:44"
}

So, the verification token/primary key becomes 0.

Can anyone please help?

Php Solutions


Solution 1 - Php

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';
}

Solution 2 - Php

On the model set $incrementing to false

public $incrementing = false;

This will stop it from thinking it is an auto increment field.

Laravel Docs - Eloquent - Defining Models

Solution 3 - Php

Theres two properties on the model you need to set. The first $primaryKey to tell the model what column to expect the primary key on. The second $incrementing so it knows the primary key isn't a linear auto incrementing value.

class MyModel extends Model
{
    protected $primaryKey = 'my_column';

    public $incrementing = false;
}

For more info see the Primary Keys section in the documentation on Eloquent.

Solution 4 - Php

keep using the id


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserVerification extends Model
{
    protected $table = 'user_verification';
    protected $fillable =   [
                            'id',
                            'email',
                            'verification_token'
                            ];
    //$timestamps = false;
    protected $primaryKey = 'verification_token';
}

and get the email :

$usr = User::find($id);
$token = $usr->verification_token;
$email = UserVerification::find($token);

Solution 5 - Php

I was using Postman to test my Laravel API.

I received an error that stated

>"SQLSTATE[42S22]: Column not found: 1054 Unknown column" because Laravel was trying to automatically create two columns "created_at" and "updated_at".

I had to enter public $timestamps = false; to my model. Then, I tested again with Postman and saw that an "id" = 0 variable was being created in my database.

I finally had to add public $incrementing false; to fix my API.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbrar JahinView Question on Stackoverflow
Solution 1 - PhpandrewtweberView Answer on Stackoverflow
Solution 2 - PhplagboxView Answer on Stackoverflow
Solution 3 - PhpWaderView Answer on Stackoverflow
Solution 4 - PhpZμ 1View Answer on Stackoverflow
Solution 5 - PhpErich MeissnerView Answer on Stackoverflow