在laravel 5中这可能吗?
public function attachable($type) { // sample: $type = 'SomeContent'; return $this->morphedByMany($type::class, 'message_attachable'); }
我收到此错误:
Dynamic class names are not allowed in compile-time ::class fetch
编辑:
[2018-06-17 22:59:13] local.ERROR: Dynamic class names are not allowed in compile-time ::class fetch {"userId":1,"email":"system@gmail.com","exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalErrorException(code: 64): Dynamic class names are not allowed in compile-time ::class fetch at U:\\www\\prado247\\app\\Models\\Message\\Message.php:25) [stacktrace] #0 {main} "}
答案 0 :(得分:2)
您必须指定类路径:
public function attachable($type)
{
// sample: $type = 'SomeContent';
return $this->morphedByMany('App\\'.$type, 'message_attachable');
}
答案 1 :(得分:0)
类型是否是类的字符串名称,还是该实例的对象?如果它是一个实例,并且您想知道它是哪个类,则应使用
return $this->morphedByMany(get_class($type), 'message_attachable');
您可以阅读get_class
in the PHP Docs的文档,但基本上它会将实例的类作为Eloquent可以使用的字符串返回。