0

I have a User and a Profile class in my application with 1:1 relationship. Inside my ProfileController@show I can instantiate and build query perfectly fine for each of them but these two lines :

$userProfile=$profile->with('user')->where('id',1)->firstOrFail();
$userProfile=$user->with('profile')->where('id',3)->firstOrFail();

The first one sends a fatal error Exception with the message: "Class User Not Found" and the message for the second one is "Class Profile Not Found"

I also tried facades and different way of building query but it didn't work.

According to this question I think there is something wrong with my relationship. I passes the foreign_key explicitly, but it doesn't work.

Here is some details:

User class:

class User extends \Eloquent implements UserInterface, RemindableInterface
{

    public function profile()
    {
        return $this->hasOne('Profile', 'id');
    }

}

Profile class

class Profile extends \Eloquent
{

    public function user()
    {
        return $this->belongsTo('User', 'id');
    }
}

Database schema: table "users"

id
password
email
username

table "profiles"

id
user_id
about

Thanks in advanced.

Community
  • 1
  • 1
artronics
  • 1,399
  • 2
  • 19
  • 28
  • Are the classes in an subfolder of models or inside an package ? Why does \Eloquent has an leading backslash? did you do an `php artisan dump-autoload` – DonSeba Nov 21 '14 at 14:23
  • They are in my namespace like so: `Artronics\User` and `Artronics\Profile` with psr-4 autoloading. yes I tried `composer dump-autoload` actually many times . – artronics Nov 21 '14 at 14:28
  • I just tried `php artisan dump-autoload` but still the same result. btw what's the difference between `composer dump-autoload` and `php artisan dump-autoload` ? – artronics Nov 21 '14 at 14:34
  • the difference is : http://stackoverflow.com/questions/20274082/what-are-differences-between-php-artisan-dump-autoload-and-composer-dump-auto – DonSeba Nov 21 '14 at 14:36

2 Answers2

3

If your Model has a namespace you need to insert the full namespaced class as a parameter. Otherwise Eloquent does not know where to find the class.

OR you need to add the models to your classmap in app/config/app.php

class User extends \Eloquent implements UserInterface, RemindableInterface
{
    public function profile()
    {
        return $this->hasOne('\Path\TO\Model\Profile', 'id');
    }
}
DonSeba
  • 2,098
  • 2
  • 16
  • 25
2

What about this?

class User extends \Eloquent implements UserInterface, RemindableInterface
{

    public function profile()
    {
        return $this->hasOne('Profile', 'user_id');
    }

}

Shouldn't it be a foreign key? http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Relations/HasOne.html

Pete Houston
  • 14,931
  • 6
  • 47
  • 60
  • Thanks @xjaphx , Since I followed all laravel conventions, actually there is no need to be explicit about foreign keys. btw I tried both `id` and `user_id` and both works! (after inserting full class path) – artronics Nov 21 '14 at 14:49
  • 1
    @SeyedJalalHosseini While accepted answer solves the namespace for you, this answer is the correct way to define relationship. Your setup 'works', but definitely not how you would expect. – Jarek Tkaczyk Nov 21 '14 at 15:08
  • Yess @xjaphx i checked the doc again. you absolutely right. thanks – artronics Nov 21 '14 at 16:25