2016-04-12 6 views
0

Ich habe Schlepptau Tabellen:undefinierte Beziehung auf Laravel 5

Benutzer (id, nom, role_id (fk), ...) Rollen (id, Rolle)

Benutzer modele i erstellen :

public function role() 
{ 
    return $this->belongsTo('App\Role','role_id'); 
} 
public function hasRole($role) 
{ 
    $role = $this->role(); 
    if (!is_null($role)) { 
     $role = $role->role; 
    } 
    return ($user_role===$role) ? true : false ; 
} 
public function whatRole() 
{ 
    $user_role = $this->roles(); 
    if (!is_null($user_role)) { 
     $user_role = $user_role->role; 
     return $user_role; 
    }else{ 
     return false; 
    } 
} 

und in Rolle modal i erstellen:

public function user() 
{ 
    return $this->hasMany('App\User'); 
} 

bu t, wenn ich versuche es so zu verwenden: Auth::user()->hasRole('medecin') erhalte ich diesen Fehler:

ErrorException in User.php line 25: Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$role (View: C:\wamp\www\Medecin2016\resources\views\pages\role_form.blade.php) 

in User.php line 25 
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 43 
at PhpEngine->evaluatePath('C:\wamp\www\Medecin2016\storage\framework\views/018276e247e24f69812c603a17c57319', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag))) in CompilerEngine.php line 57 
at CompilerEngine->get('C:\wamp\www\Medecin2016\resources\views/pages/role_form.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag))) in View.php line 142 
at View->getContents() in View.php line 111 
at View->renderContents() in View.php line 80 
....................... 
+0

können Sie die vollständige Modellklasse einfügen – osleonard

Antwort

0

Ich glaube, Sie machen einige Verwirrung mit Ihrem Modell ... Mit dem hasRole Methode, die Sie überprüfen möchten, ob der Benutzer hat die angegebene Rolle (in deinem Fall "medicin"). Lassen Sie uns Ihren Code überprüfen, um zu verstehen, was Sie wirklich falsch machen:

public function hasRole($role) 
{ 
    // Here you override the given string: this means that you actually 
    // lose the 'medicin' value. Additionally, you're calling a method 
    // that defines the Eloquent relation. There is NO NEED to do 
    // anything like that. Maybe you want to call the $this->role 
    // property instead of the method... 
    $role = $this->role(); // REMOVE THIS 

    // Here you should check if the given string is empty or not 
    if (!is_null($role)) { // Change is_null with empty is better (IMHO) 
     // Again, you're overriding the given string ('medicin') 
     // 
     // $role = $role->role; 
    } 

    // You are returning the Boolean value according to the result 
    return ($user_role===$role) ? true : false ; 
} 

Jetzt wollen wir versuchen, besser, etwas zu tun: Ändern Sie Ihre Methode, wie der Trick sollte Folgendes:

public function hasRole($role = '') 
{ 
    if (!empty($this->role) && $role === $this->role->{FIELD}) { 
     return TRUE; 
    } 

    return FALSE; 
} 

Ändern Sie den { FELD} mit dem korrekten Feld in der Rollentabelle. Hoffe, das wird helfen.