我有一个 MorphTo - MorphMany 关联关系,一个地址可以属于用户或组织机构。我一直收到错误消息:
调用未定义的方法 App\HTTP\Resources\User::singularLabel()
我尝试使用静态方法,但似乎没有帮助。下面是文件的代码。
谢谢!
Address.php 模型
public function addressable()
{
return $this->morphTo('addressable');
}
public function getHashidsConnection()
{
return 'address';
}
User.php(模型)
public function organisation()
{
return $this->belongsTo('App\Organisation');
}
public function addresses()
{
return $this->morphMany('App\Address', 'addressable');
}
Address.php(资源文件)
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('line_1'),
Text::make('line_2'),
Text::make('line_3'),
Text::make('post_code'),
Boolean::make('is_primary'),
MorphTo::make('Addressable')->types([
User::class,
Organisation::class,
])
];
}
public static function singularLabel()
{
return Address::class;
}
User.php (Resource)
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Gravatar::make(),
Text::make('Name')
->sortable()
->rules('required', 'max:255'),
Text::make('Email')
->sortable()
->rules('required', 'email', 'max:254')
->creationRules('unique:users,email')
->updateRules('unique:users,email,{{resourceId}}'),
Password::make('Password')
->onlyOnForms()
->creationRules('required', 'string', 'min:8')
->updateRules('nullable', 'string', 'min:8'),
MorphMany::make('Addresses')
];
}