【问题标题】:Laravel Find Not working with custom primary keyLaravel Find 不使用自定义主键
【发布时间】:2018-06-17 20:27:39
【问题描述】:

我遇到了这样一种情况,即我无法在 Laravel 5.5 中使用自定义主键使用 Eloquent 更新我的记录。
这是我尝试过的:-
在我的 Model.php 中,

protected $primarykey = 'custom_primary_key';

我的迁移看起来像:-
$table->increments('custom_primary_key');

在我的控制器中,我有:-

Model::find(custom_primary_key);

但是当我尝试在我的控制器中使用find() 查找记录时,它给出了

Column not found: 1054 Unknown column 'table.id' in 'where clause' (SQL: select * from `table` where `table`.`id` = 1 and `table`.`deleted_at` is null limit 1)"   

表还包含一个具有自定义主键的列,并且没有名为 id 的列。

它不识别自定义主键。我在哪里可以错过一些东西?

【问题讨论】:

  • 尝试做K大写$primaryKey

标签: laravel primary-key


【解决方案1】:

将 $primarykey 设置为 $primaryKey。在此处查看 Laravel 的文档: https://laravel.com/docs/5.6/eloquent#eloquent-model-conventions

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-31
    • 2021-12-12
    • 2018-12-30
    • 2018-06-09
    • 2014-07-30
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    相关资源
    最近更新 更多