我的情況是:一個日曆屬於客戶或推銷員Laravel - 機鋒:與命名空間
因爲我也有類,如事件和文件,我用我所有的模型類的命名空間的應用程序\模型。
,所以我成立了多態的關係:
在Calender.phppublic function user() {
return $this->morphTo();
}
在Customer.php和Salesman.php
public function calendars() {
return $this->morphMany('App\Models\Calendar', 'user');
}
現在,當我做
$calendar= Calendar::find(1); //calendar from a salesman
$calendar->user; //error here
...
我得到t他的錯誤消息:
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class 'salesman' not found
我注意到,'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';
}
$instance = new $related; //HIGHLIGHTED
在這一行之前,我遇到了類似的錯誤,當時我正在搞亂命名空間,所以我猜這跟它有關。有什麼辦法可以告訴morphTo()
方法使用正確的名稱空間嗎?
或者這是別的什麼原因造成這個問題?
也發現了這個解決方案,但似乎無法得到它的工作: Polymorphic Eloquent relationships with namespaces
你可以看到插入數據庫和查詢過程中,記錄您的查詢,看看是在查詢發送。 – Shafiul
問題是查詢沒有被執行,因爲他找不到'顧問'。當我手動(通過覆蓋morphTo())給出命名空間時,該查詢被執行,但使用'App \ Models \ Advisor'來檢查類型值... SUCKS –