2016-07-07 13 views
1

Ich habe, wie unten ein User-Modell bekam:Laravel Versuch Eigenschaft Nicht-Objekt, Beziehung innerhalb gleichen Tisch zu bekommen

<?php 

namespace App; 

use Illuminate\Auth\Authenticatable; 
use Illuminate\Database\Eloquent\Model; 
use Illuminate\Auth\Passwords\CanResetPassword; 
use Illuminate\Foundation\Auth\Access\Authorizable; 
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; 
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; 
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; 

class User extends Model implements AuthenticatableContract, 
            AuthorizableContract, 
            CanResetPasswordContract 
{ 
    use Authenticatable, Authorizable, CanResetPassword; 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'users'; 
    public function recruits() 
    { 
     return $this->hasMany('App\User','recruiters_id'); 
    } 
    public function recruiter() 
    { 
     return $this->belongsTo('App\User','recruiters_id'); 
    } 
} 

Es gibt einen ‚recruiters_id‘ Feld in der Tabelle ‚Benutzer‘, das war eingerichtet mit:

public function up() 
    { 
     Schema::table('users', function (Blueprint $table) { 
      $table->foreign('recruiters_id')->references('id')->on('users'); 
     }); 
    } 

Die Idee ist also, dass Benutzer andere Benutzer rekrutieren können. Aber wenn ich etwas wie {{$ user-> recruiter-> id()}} versuche, bekomme ich die Fehlermeldung "Versuch, Eigenschaft von Nicht-Objekt zu bekommen"

Wenn ich {{print_r ($ user-> recruiter)}} stattdessen bekomme ich die Ausgabe unten, die wie ein Objekt aussieht (zumindest für meine unerfahrenen Augen, obwohl es scheint, dass das "Objekt" ein paar Mal wiederholt wird, obwohl ich nur print_r einmal verwende):

App\User Object ([table:protected] => users [fillable:protected] => Array ([0] => review_count [1] => review_sum [2] => review_avg [3] => display_name [4] => business_name [5] => setting1 [6] => referrer_id [7] => referrer_margin [8] => gender [9] => age [10] => title [11] => points [12] => credits [13] => bankacct_nr [14] => bankacct_name [15] => bsb [16] => save_payment [17] => newsletter [18] => first_name [19] => last_name [20] => phone [21] => email [22] => password [23] => subject [24] => credentials [25] => margin [26] => user_type [27] => url [28] => questions [29] => form_subject [30] => postcode [31] => recruiters_id [32] => internal_notes) [hidden:protected] => Array ([0] => password [1] => remember_token) [connection:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ([id] => 240 [first_name] => Jane [last_name] => Recruit [title] => [phone] => [email] => [email protected] [user_type] => recruiter [margin] => 0.00 [credentials] => [password] => $2y$10$eTSX9vRGFr5eupAL/kyexeARVDjT.M/7xE2Yp.6PMTuH/dOsFd6QS [remember_token] => [created_at] => 2016-07-06 15:19:23 [updated_at] => 2016-07-06 15:19:23 [status] => [url] => [newsletter] => 0 [postcode] => [form_subjects] => [questions] => [save_payment] => 0 [bsb] => [bankacct_nr] => [bankacct_name] => [points] => 0 [credits] => 0 [gender] => [age] => 0 [referrer_id] => [referrer_margin] => 0.00 [setting1] => [business_name] => [display_name] => i recruit [following] => [followers] => [belts_id] => [recruiters_id] => [internal_notes] => [review_count] => 0 [review_sum] => 0 [review_avg] => 0.00) [original:protected] => Array ([id] => 240 [first_name] => Jane [last_name] => Recruit [title] => [phone] => [email] => [email protected] [user_type] => recruiter [margin] => 0.00 [credentials] => [password] => $2y$10$eTSX9vRGFr5eupAL/kyexeARVDjT.M/7xE2Yp.6PMTuH/dOsFd6QS [remember_token] => [created_at] => 2016-07-06 15:19:23 [updated_at] => 2016-07-06 15:19:23 [status] => [url] => [newsletter] => 0 [postcode] => [form_subjects] => [questions] => [save_payment] => 0 [bsb] => [bankacct_nr] => [bankacct_name] => [points] => 0 [credits] => 0 [gender] => [age] => 0 [referrer_id] => [referrer_margin] => 0.00 [setting1] => [business_name] => [display_name] => i recruit [following] => [followers] => [belts_id] => [recruiters_id] => [internal_notes] => [review_count] => 0 [review_sum] => 0 [review_avg] => 0.00) [relations:protected] => Array () [visible:protected] => Array () [appends:protected] => Array () [guarded:protected] => Array ([0] => *) [dates:protected] => Array () [dateFormat:protected] => [casts:protected] => Array () [touches:protected] => Array () [observables:protected] => Array () [with:protected] => Array () [morphClass:protected] => [exists] => 1 [wasRecentlyCreated] =>) 1 

Das ist das erste Mal, dass ich eloquente Beziehungen innerhalb der gleichen Tabelle aufstelle, also habe ich vielleicht ein paar Fehler gemacht. Laravel 5.1

Mit würde Jede Beratung sehr geschätzt werden, Dank im Voraus

+0

Können Sie Ihren Benutzer Tabelle Migration Code (die vollständige) –

Antwort

2

Ohne die vollständige Migration mit mir das Problem würde sagen, liegt hier

public function recruits() 
{ 
    return $this->hasMany('App\User','recruiters_id'); 
} 
public function recruiter() 
{ 
    return $this->belongsTo('App\User','recruiters_id'); 
} 

für beiden Beziehungen Ihrer give recruites_id als fremd Schlüssel, obwohl es id Rekruten ist.

für hasMany sollten Sie

return $this->hasMany('App\User', 'foreign_key', 'local_key'); 

Das Gleiche gilt für belongsTo

return $this->belongsTo('App\User', 'foreign_key', 'other_key'); 

Deshalb haben

public function recruits() 
{ 
    return $this->hasMany('App\User','recruiters_id', 'id'); 
} 
public function recruiter() 
{ 
    return $this->belongsTo('App\User'); 
} 

Sollte das sein, was Sie suchen (IF ist recruiters_id richtig in der Migration definiert)

+0

Vielen Dank! Das hat mein Problem gelöst – user6122500