Laravel 5.5具有新的API资源功能,可以很好地将调用重定向到模型属性(如org.eclipse.persistence:javax.persistence:jar:2.2.0
)。我使用[INFO] | \- org.eclipse.persistence:eclipselink:jar:2.7.0:compile
[INFO] | +- org.eclipse.persistence:javax.persistence:jar:2.2.0:compile
[INFO] | +- org.eclipse.persistence:commonj.sdo:jar:2.1.1:compile
[INFO] | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | \- org.glassfish:javax.json:jar:1.0.4:compile
为类型提示所有模型属性的模型生成phpdoc。但是,这不适用于资源,我得到“通过魔术方法访问的字段”波浪形。有没有办法将它指向模型的phpdoc而不复制它?
答案 0 :(得分:9)
您可以使用 @mixin
以下是一个示例,如果您想要用户资源中用户模型的属性/ phpdocs,那么就这样做
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
/**
* Class User
*
* @mixin \User
* */
class User extends Resource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}