database - Eloquent 问题 : Undefined function: 7 ERROR: operator does not exist

标签 database laravel postgresql eloquent

我使用postgresql并且我有三个表

1:适应能力

code              - string(225)
code_menu  - string(225) 
code_sub_menu  - string(225) 
name             - string(225)

2:菜单

code              - string(225)
name             - string(225)

3:子菜单

code              - string(225)
name             - string(225)

当我尝试时,我想使用 Eloquent 来显示他们的菜单子菜单习惯:

Habilitation::with(['menu','submenu'])->get();

我收到此错误:

Illuminate\Database\QueryException
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: character varying = integer LINE 1: select * from "menu" where "menu"."code" in (0, 0, 0, 0) ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts. (SQL: select * from "menu" where "menu"."code" in (0, 0, 0, 0)) 

我的模型:

class Menu extends Model
{
    protected $table = "menu";
    public $primaryKey = "code";
    public $timestamps = false;

    protected $casts = [
        'code' => 'string',
    ];

    public function submenu()
    {
        return $this->hasMany('App\Models\SubMenu','code');
    }

    public function habilitation()
    {
        return $this->hasMany('App\Models\Habilitation','code');
    }
}

class SubMenu extends Model
{
    protected $table = "submenu";
    public $primaryKey = "code";
    public $timestamps = false;

    protected $casts = [
        'code' => 'string',
    ];

    public function menu()
    {
        return $this->belongsTo('App\Models\SubMenu','code');
    }

    public function habilitation()
    {
        return $this->hasMany('App\Models\Habilitation','code');
    }

}

class Habilitation extends Model
{
    protected $table = "habilitation";
    public $primaryKey = "code";
    public $timestamps = false;

    protected $fillable = [
        'code', 'code_menu','code_sub_menu'
    ];

    protected $casts = [
        'code' => 'string',
        'code_menu' => 'string',
        'code_sub_menu' => 'string'
    ];

    public function menu()
    {
        return $this->belongsTo('App\Models\Menu','code_menu','code');
    }

    public function submenu()
    {
        return $this->belongsTo('App\Models\SubMenu');
    }

}

我使用 Laravel 的查询构建器没有问题,但我想使用 Eloquent。 有人可以帮助我吗?或者给我一些提示?
预先感谢您

最佳答案

我添加了 public $keyType = ‘string’; 来解决此问题。 提示:https://www.tekmx.com/blog/using-non-standard-primary-key-with-eloquent-relations-laravel-5

关于database - Eloquent 问题 : Undefined function: 7 ERROR: operator does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59574496/

相关文章:

c# - 截断表 C# 找不到表 - 不存在或没有权限

java - 如何在JSP中使用neo4j

mysql - Laravel 与不同值、产品属性的多对多关系

sql-server - SQL Server : Importing database from . MDF?

php - 在 Heroku 中部署 Laravel 项目后 MySQL 不起作用

laravel - 代码:[RuntimeException]调用未定义的方法AcceptanceTester::wait

ruby - Distinct 不适用于 select 子句

postgresql - Liquibase 不会将间隔添加到 current_timestamp

java - org.hibernate.exception.SQLGrammarException 不正确的 postgresql sql 查询

java连接不上mysql数据库