我使用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/