2016-06-18 5 views
1

Hier meine Tabellenstrukturen sind:Warum ist mein eifriger Laden mit Fehlern "BadMethodCallException mit der Meldung‚Call to undefined Methode Illuminate Database Abfrage Builder :: Profil()"

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 

Das Modell 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'); 
    } 
} 

Das Modell 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'); 
    } 

} 

Das Modell 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 Handwerker basteln“, bekomme ich folgende Ergebnisse

$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: "dsmith1[email protected]", 
     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()' 

Warum kann ich nicht eifrig Last Länder mit Profile oder Profile mit den Ländern? Kann mir bitte jemand erklären.

+0

In "Land" ist der Name Ihrer Methode "Profile", aber Sie rufen "Profil" (ohne "s") –

+0

Auf Profilmodell, da Sie "country_id" als Fremdschlüssel verwenden, müssen Sie nicht Verwenden Sie den zweiten und dritten Parameter. –

+0

Ich habe es letzte Nacht mit App \ Country :: with ('profiles') versucht und auch das hat nicht funktioniert. Ich habe eine neue Instanz von php artisan tinker gestartet und festgestellt, dass das alles jetzt funktioniert. Irgendeine Idee, warum es vielleicht nicht funktioniert hat, als ich es das erste Mal versucht habe? – user3125602

Antwort

0

Laut meinem letzten Kommentar, ohne Änderung des Codes, hat sich das Problem selbst gelöst. Ich entschuldige mich dafür, dass ich keine präskriptive Lösung anbieten konnte, das ursprüngliche Problem ist jedoch gelöst. Ravisha Hesh könnte ein Argument dafür sein, dass es etwas mit Autoloading zu tun hat. Ich vermute, obwohl es keine direkten Beweise gibt, dass es etwas mit der Registrierung von etwas zu tun haben könnte.

Ich entschuldige mich bei allen und erkenne völlig, dass dies keine befriedigende Antwort darstellt.

Verwandte Themen