今天我碰到了一个问题。基本上我想使用两个表从数据库,做一些计算的方法,然后返回它作为一个Type
。然而,我得到错误,我不需要把它存储在任何地方,它应该是临时的,只是发送信息到前端。我试图使用NormalWorkHours
和OverrideWorkHours
表来获得一个信息表。我要创建模型实例,然后发送数据还是用字符串,json之类的?这是我的WorkHours
模型:
class WorkHours extends Model
{
use HasFactory, Uuids;
protected $fillable = [
'dayofweek',
'open_time',
'close_time',
'timezone',
];
public function restaurant()
{
return $this->belongsTo(Restaurant::class);
}
}
这是餐厅模型:
class Restaurant extends Model
{
use HasFactory, Uuids;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'selected'
];
public function user()
{
return $this->belongsTo(User::class);
}
public function category()
{
return $this->belongsToMany(RestaurantCategory::class);
}
public function address()
{
return $this->hasOne(Address::class);
}
public function normalWorkHours()
{
return $this->hasMany(NormalWorkHours::class);
}
public function overrideWorkHours()
{
return $this->hasMany(OverrideWorkHours::class);
}
public function getWorkHours()
{
// $workHours = new WorkHours(
// [
// 'dayofweek' => $this->normalWorkHours[0]->dayofweek,
// 'open_time' => $this->normalWorkHours[0]->open_time,
// 'close_time' => $this->normalWorkHours[0]->close_time,
// 'timezone' => $this->normalWorkHours[0]->timezone,
// ]
// );
$workHours = new WorkHours;
$workHours->dayofweek = $this->normalWorkHours[0]->dayofweek;
$workHours->open_time = $this->normalWorkHours[0]->open_time;
$workHours->close_time = $this->normalWorkHours[0]->close_time;
$workHours->timezone = $this->normalWorkHours[0]->timezone;
return $workHours;
}
}
我还没有做逻辑,计算,只是测试。我试图创建WorkHours
的新对象并将其作为graphql中的对象返回。但是我得到错误:
"debugMessage": "User Error: expected iterable, but did not find one for field Restaurant.getWorkHours."
这是我的餐厅类型:
type Restaurant {
id: ID!
user_id: ID!
name: String!
selected: Boolean
image_path: String
created_at: DateTime!
updated_at: DateTime!
user: User! @belongsTo
category: [RestaurantCategory!] @belongsToMany
ingredients: [Ingredient] @hasMany
additions: [AdditionsCollection!] @hasMany
alergens: [Alergens] @hasMany
extraIngredients: [ExtraIngredients] @hasMany
mealSizes: [MealSize!] @hasMany
sauces: [Sauces!] @hasMany
address: Address! @hasOne
normalWorkHours: [NormalWorkHours] @hasMany
overrideWorkHours: [OverrideWorkHours] @hasMany
# getWorkhours: WorkHours @method(name: "getWorkHours")
getWorkHours: [WorkHours] @method
}
我也有类型的WorkHours
在graphql.
type WorkHours {
dayOfWeek: Int @rename(attribute: "dayofweek")
openTime: String @rename(attribute: "open_time")
closeTime: String @rename(attribute: "close_time")
timezone: Int
closed: Boolean!
}
您的模式说Restaurant.getWorkHours
将返回一个列表,但您的方法只返回单个对象-因此错误消息:
"debugMessage": "User Error: expected iterable, but did not find one for field Restaurant.getWorkHours."