Laravel 5字符串作为主键

3qpi33ja  于 2023-01-10  发布在  其他
关注(0)|答案(1)|浏览(146)

我正在做一个使用Laravel 5.5的项目,其中一个模型的ID是由用户定义的,在模型下面,将$incrementing设置为false,并将其放在$fillable数组中。

class AIOpportunity extends Model
{
    protected $table = 'ai_opportunity';
    protected $primaryKey = 'id_opportunity';
    public $incrementing = false;

    protected $fillable = [
        'id_opportunity',
        'budget',
        'created_by'
    ];
}

问题是:尝试执行此查询时:

$rec = AIOpportunity::where('id_opportunity', 'abc')->first();

它在数据库中执行了以下查询:

select * from ai_opportunity where id_opportunity=abc

这会因为类型而抛出一个错误。我还能做些什么让Eloquent理解这是一个varchar字段吗?

dldeef67

dldeef671#

我发现了这个问题。这不是一个Laravel的问题,我的问题中的设置是正确的。问题是在我本地的数据库中。虽然表存在,当我通过PhPMyAdmin访问时,它看起来很好,我可以看到它,但也有错误消息说,表不存在。
是的,非常奇怪。我刚刚删除了本地数据库,并从生产中恢复了备份。它工作正常。问题出在数据库本身,而不是Laravel应用程序。

相关问题