[英]CodeIgniter Eloquent Model Relationship Class Model Not Found
我正在嘗試將 eloquent 應用到我的 CodeIgniter 項目中。
當我在我的項目中調用模型時它工作得很好(它從我的數據庫中給我數據)。 但是當我嘗試為其模型調用關系時,它出錯並顯示以下消息:
An uncaught Exception was encountered
Type: Error
Message: Class 'application\models\Profile' not found
Filename: /var/www/public/chupspace-git/vendor/illuminate/database/Eloquent/Model.php
Line Number: 750
Backtrace:
File: /var/www/public/chupspace-git/application/models/User.php
Line: 12
Function: hasOne
File: /var/www/public/chupspace-git/vendor/illuminate/database/Eloquent/Model.php
Line: 2638
Function: profile
File: /var/www/public/chupspace-git/vendor/illuminate/database/Eloquent/Model.php
Line: 2573
Function: getRelationshipFromMethod
File: /var/www/public/chupspace-git/vendor/illuminate/database/Eloquent/Model.php
Line: 3263
Function: getAttribute
File: /var/www/public/chupspace-git/application/controllers/UserController.php
Line: 53
Function: __get
File: /var/www/public/chupspace-git/index.php
Line: 357
Function: require_once
我在我的composer.json
使用"illuminate/database": "5.0.28",
庫
這是我的模型
User.php
if (!defined('BASEPATH')) exit('No direct script access allowed');
use \Illuminate\Database\Eloquent\Model as Eloquent;
class User extends Eloquent {
protected $table = "users";
public function profile()
{
return $this->hasOne('application\models\Profile');
}
}
Profile.php
if (!defined('BASEPATH')) exit('No direct script access allowed');
use \Illuminate\Database\Eloquent\Model as Eloquent;
class Profile extends Eloquent {
protected $table = "user_profiles";
}
這是我的目錄映射。
讓我知道是否缺少信息,以便我可以更新此線程。 你的幫助對我來說非常寶貴。 謝謝。
您需要在使用之前加載所有關系類。
首先,您需要在application/config/config.php
啟用 composer 自動加載。 只需將$config['composer_autoload']
為TRUE
然后你需要擴展你的composer.json
來加載每個模型:
{
"autoload": {
"classmap": ["application/models/"]
}
}
然后你需要重新生成自動加載文件(在 cli 中運行):
composer dump-autoload
它應該是作品。 還有更多其他方法可以做到這一點,但我認為這是最清楚的。
我找到了解決方案。 我們只需要改變
$this->hasOne('application\\models\\Profile');
成為
$this->hasOne(new Profile());
這是我的完整代碼。
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
use \Illuminate\Database\Eloquent\Model as Eloquent;
class User extends Eloquent {
protected $table = "users";
public function profile()
{
// dd();
return $this->hasOne(new Profile());
}
}
它對我很有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.