samedi 18 juin 2016

Why is my eager loading failing with "BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::profile()"

Here are my table structures:

users

id INT 10
name VARCHAR 255
email VARCHAR 255
password VARCHAR 255
remember_token VARCHAR 255
created_at TIMESTAMP
updated_at TIMESTAMP

profiles

id INT 10
user_id INT 11
address1 VARCHAR 255
address2 VARCHAR 255
city VARCHAR 255
postcode VARCHAR 255
country_id INT 11
location VARCHAR 255
created_at TIMESTAMP
updated_at TIMESTAMP

countries

id INT 10
name VARCHAR 255
currency VARCHAR 255
currency_code CHAR 3
iso2 CHAR 2
iso3 CHAR 3
locale VARCHAR 255

The model App\User.php

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Relating profile to user
     * 
     * @return type
     */
    public function profile()
    {
        return $this->hasOne('App\Profile');
    }
}

The model App\Profile.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{

    public function user()
    {
        return $this->belongsTo(User::class);
    }



    /**
     * Relating country to profile
     * 
     * @return type
     */
    public function country()
    {
        return $this->belongsTo('App\Country' ,'country_id', 'id');
    }

}

The model App\Country.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Country extends Model
{


    /**
     * Relating profile to country
     * 
     * @return type
     */
    public function profiles()
    {
        return $this->hasMany('App\Profile', 'country_id');
    }

}

In "php artisan tinker", I get the following results

$profile = App\Profile::with('user')->whereId(1)->first();
=> App\Profile {#762
     id: 1,
     user_id: 1,
     address1: "8 Acacia Way"
     address2: null,
     city: "Sheffield",
     postcode: "S9 7TJ",
     country_id: 198,
     location: null,
     created_at: "2016-06-17 22:03:11",
     updated_at: "2016-06-17 22:03:11",
     user: App\User {#767
       id: 1,
       name: "David Smith",
       email: "dsmith1865@hotmail.com",
       created_at: "2016-06-16 22:15:43",
       updated_at: "2016-06-16 22:15:43",
     },
   }

$profile = App\Profile::with('country')->whereId(1)->first();
Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'countries.profile_id' in 'where clause' (SQL: select * from `countries` where `countries`.`profile_id` in (1))'

$country = App\Country::with('profile')->whereId(198)->first();
BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::profile()'

Why can't I eager load countries with profiles or profiles with countries? Can someone please explain.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire