我是Laravel Eloquent ORM使用PgSql,当我使用with()
方法是返回错误。
SQLSTATE [42883]:未定义函数:7错误:操作员不存在:uuid = integer行1:
Laravel版本10.7.1 Php版本8.1.17
下面是表的结构,由于某种原因,我改变了表名。
parents
--------
id (uuid)
name (string)
user_id (uuid)
description (text)
details
---------
id (uuid)
parent_id (uuid)
title (stirng)
description (tinytext)
下面是来自控制器的代码
/**
* Display the specified resource.
*/
public function show(string $id)
{
$this->authorize('viewParent', \App\Models\Parent::class);
$model = \App\Models\Parent::with(['user', 'parentChilds'])->findOrFail($id);
return view("$this->view.show", [
'model' => $model,
'title' => "$this->title",
'route' => $this->route,
]);
}
但是当我使用$modelParent->parentChilds()->createMany([...data])
方法时,它没有返回任何错误。
父模型
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'user_id',
'description',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'string',
];
/**
* Get the user that owns the MinuteOfMeeting
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id')->withTrashed();
}
/**
* Get all of the minuteOfMeetingDetails for the MinuteOfMeeting
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function parentChilds(): HasMany
{
return $this->hasMany(ParentChild::class, 'parent_id', 'id');
}
子模型
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'parent_id',
'title',
'description'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'string',
'parent_id' => 'string',
];
/**
* Get the minuteOfMeeting that owns the MinuteOfMeetingDetail
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent(): BelongsTo
{
return $this->belongsTo(Parent::class, 'parent', 'id')->withTrashed();
}
1条答案
按热度按时间x759pob21#
正如@Frank Heikens的评论中提到的,您需要验证输入是否是UUID。
您需要在子模型上显式地声明表名。
在您的子模型中,方法
parent
的第二个参数应该是parent_id
,而不是parent
。文档