GreenPepper
GreenPepper

Reputation: 138

Laravel changing of primaryKey don't work

I'm trying to change the primaryKey of my Model in Laravel. The primaryKey is already set in Database, and in the Model, but both doesn't work.

I get this error message:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause' (SQL: select * from users where `` = 123456 limit 1)

When I unset the primaryKey within the Model, I get this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.id' in 'where clause' (SQL: select * from users where users.id = 123456 limit 1)

Which is totally legit, because the primaryKey should be set to 'vid' within the Model.

Here is the essential part of my Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class User extends Model implements \Illuminate\Contracts\Auth\Authenticatable
{
    use Notifiable;

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'vid' => 'string'
    ];

    protected $table = 'users';
    protected $primaryKey = 'vid';
    public $incrementing = false;

}

Migration:

        Schema::create('users', function (Blueprint $table) {
            $table->string('vid');
            $table->string('name');
            $table->string('surname');
            $table->integer('val1');
            $table->integer('val2');
            $table->integer('val3');
            $table->string('val4');
            $table->string('val5');
            $table->integer('val6');
            $table->integer('val7');
            $table->integer('val8');
            $table->dateTime('val9');
            $table->string('val10');
            $table->string('val11');
            $table->timestamps();
        });

Upvotes: 1

Views: 683

Answers (1)

GreenPepper
GreenPepper

Reputation: 138

I really can't tell you what was wrong, but remaking the User Model helped.

Upvotes: 0

Related Questions