简体   繁体   中英

CodeIgniter Eloquent Model Relationship Class Model Not Found

i'm trying to implement eloquent into my CodeIgniter project.

It works good when i call a model in my project (it give me data from my database). But when i'm trying to call relation for its model, it's went wrong and show this message:

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

I use "illuminate/database": "5.0.28", library in my composer.json

Here is my model

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";
}

And here is my directory map.

在此处输入图片说明

Let me know if there is missing of information, so i can update this thread. Your help is very valuable to me. Thanks.

You need to load all relation class before use it.

First of all you need to enable in the application/config/config.php the composer autoload. Just set $config['composer_autoload'] to TRUE

Then you need to extend your composer.json to load every models:

{
"autoload": {
    "classmap": ["application/models/"]
}

}

Then you need to regenerate the autoload file (run in cli):

composer dump-autoload

It should be works. There is more other ways to do it, but i think it's the clearest.

I found the solution. We just need to change

$this->hasOne('application\\models\\Profile');

to be

$this->hasOne(new Profile());

Here is my full code.

<?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());
    }
}

It works good for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM