小编Tho*_*ens的帖子

Laravel - Eloquent:与命名空间的多态关系

我的情况是:日历属于客户或推销员

因为我也有像Event和File这样的类,所以我使用名称空间App\Models作为我所有的模型类.

所以我建立了多态关系:

在Calender.php中

public function user() {
    return $this->morphTo();
}
Run Code Online (Sandbox Code Playgroud)

在Customer.php和Salesman.php中

public function calendars() {
    return $this->morphMany('App\Models\Calendar', 'user');
}
Run Code Online (Sandbox Code Playgroud)

现在当我这样做

$calendar= Calendar::find(1); //calendar from a salesman
$calendar->user; //error here
...
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class 'salesman' not found
Run Code Online (Sandbox Code Playgroud)

我注意到这'salesman'是低壳,也许这是问题?

这就是我从Laravels堆栈跟踪中获得的

打开:/var/www/cloudcube/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php

// foreign key name by using the name of the relationship function, which
// when combined with an "_id" should conventionally match the columns.
if (is_null($foreignKey))
{
    $foreignKey = snake_case($relation).'_id'; …
Run Code Online (Sandbox Code Playgroud)

namespaces polymorphic-associations eloquent laravel-4

1
推荐指数
1
解决办法
3221
查看次数