1
votes

Using Laravel 5.4.13, I have a model called Widget, where I set the primary key like this:

protected $primaryKey = 'widget_id';

The widgets table has a primary key column, int, auto-increment, named: widget_id.

Yet, when I try to update a record on the browser I get this error:

QueryException in Connection.php line 647:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from widgets where name = est eum and id <> 3)

It still looks for a column called: id and not widget_id.

I found out it happens on this validation:

$this->validate($request, [
        'name' => 'required|string|max:40|unique:widgets,name,' .$widget->widget_id
    ]);

What am I missing?

4

4 Answers

1
votes

This might be a closed issue but I encountered the same and I wish to contribute based on the new approach I have taken to fix the issue:

Laravel allows you to specify the foreignkey to be used in the relationship model using:

return $this->hasOne('App\Model_name','foreign_key');

Once that is done, you can then extend the model and pull back associated records without having a trouble of specifying the unique fields.

To check more on this follow documentation.

0
votes

try this:

$this->validate($request, [
    'name' => 'required|string|max:40|unique:widgets,name,' .$widget->widget_id.',widget_id'
]);
0
votes

How did you actually added this new widget_id? And what have you done with the id field? If I understand well you want to remove id and replace it with widget_id right?

Keep in mind that you cannot have two auto increment field in the same table, and so on if you want to work properly you want widget_id to be AI. ( you can use public $incrementing = false; in your model but I dont recommend that )

I recommend removing the id and adding the widget_id with a new migration. To create a migration use the artisan command:

php artisan make:migration alter_user

Then add

public function up()
{
    Schema::table(User::TABLE, function(Blueprint $table)
    {
        $table->dropColumn('id');
    });
    Schema::table(User::TABLE, function(Blueprint $table)
    {
        $table->increments('widget_id');
    });
}

public function down()
{
    Schema::table(User::TABLE, function(Blueprint $table)
    {
        $table->dropColumn('widget_id');
    });
    Schema::table(User::TABLE, function(Blueprint $table)
    {
        $table->increments('id');
    });
}

Note: you cannot add them to the same Schema.

Then you add protected $primaryKey = 'widget_id'; to your model. Make sure you extend Illuminate\Database\Eloquent\Model.

It worked for me for a fresh laravel 5.4 installation, making the modifications on the user table. Hope it helps for you.

0
votes

I faced the same issue with Laravel 5.4 and actually the

protected $primaryKey = 'widget_id';

seems not to work anymore. So what I did to solve the problem is re-write all the Models with the specific custom private and foreign keys. For example read the docs for the one-to-one model about the foreign and local key here: https://laravel.com/docs/5.4/eloquent-relationships#one-to-one

Basically you have to define manually the foreign and local key on the models. So if we have a Widget and User table with a one-to-one relationship, change the model as follows: In the User table

return $this->hasOne('App\Widget', 'foreign_key_of_widget_table')

and in the widget model:

return $this->hasOne('App\User', 'foreign_key_of_users_table', 'local_key_of_widgets_table');

I had to re write everything in my project. Hope this helps.