2014-06-17 6 views
8

Ich versuche, eine HasManyTrough-Beziehung mit Eloquent einzurichten, aber es ist unklar, wie es von den Dokumenten funktioniert.Hat viele durch mit Pivot-Tabelle in Laravel4

Tables:

users 
    - id 
    - firstname 
    - ...etc 

accounts 
    - id 
    - user_id 
    - username 
    - ...etc 

roles 
    - id 
    - permissions 

account_role 
    - id 
    - account_id 
    - role_id 

Modelle

<?php 
class User extends Eloquent { 

    public function account() 
    { 
     return $this->hasOne('Account'); 
    } 

    // This is what I'm trying to achieve 
    public function roles() 
    { 
     return $this->hasManyThrough('Role', 'Account'); 
    } 
} 

class Role extends Eloquent { 

    public function accounts() 
    { 
     return $this->belongsToMany('Account')->withTimestamps(); 
    } 
} 

class Account extends Eloquent { 

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

Fehler und Frage Der Fehler Ich bin gettings ist: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles.account_id' in 'on clause'

Ich weiß, ich kann etwas tun $user->account->roles, aber ich möchte $user->roles tun können. Wie richte ich das richtig ein?

+0

'hasManyThrough' funktioniert nicht mit Viele-zu-viele-Beziehung, überprüfen Sie diese Antwort: http://stackoverflow.com/questions/23788844/hasmanythrough-with-on-to-many-relationship/23789210 # 23789210. Wenn Sie noch Fragen haben, zögern Sie nicht zu fragen. –

+0

Schöne Workaround, aber ich brauche eifrig laden mit dem Benutzermodell zu arbeiten. Vielleicht gibt es eine Lösung bei der Definition meiner eigenen "Beziehung" oder so. Ich bin mir nicht sicher, wie ich das machen soll. – DerLola

+0

Sicher können Sie Eloquent erweitern und benutzerdefinierte Beziehung, Modellmethoden und so etwas erstellen. Es liegt an dir, die Antwort, die ich verlinkt habe, sollte dir eine Idee geben, wie das geht. Ich habe genau das Gleiche gemacht, weil "goesToThrough" andersherum funktioniert. –

Antwort

9

Am Ende war ich in der Lage dies folgend, indem Sie die lösen:

class User extends Eloquent { 

    /** 
    * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany 
    */ 
    public function roles() 
    { 
     return $this->account->belongsToMany('Navicula\Entities\Role'); 
    } 
} 

Ich vermuten, dies würde die Konten nicht eifrig Last, viele Abfrage-Overhead zu verursachen. mit User::with('roles')->limit(100)->get(); und protokolliert Allerdings, wenn ich es ausprobiert, die Abfragen, liefen die folgenden Abfragen:

SELECT * FROM `users` WHERE `users`.`deleted_at` IS NULL LIMIT 100 
SELECT * FROM `accounts` WHERE `accounts`.`deleted_at` IS NULL LIMIT 1 
SELECT `roles`.*, `account_role`.`account_id` as `pivot_account_id`, `account_role`.`role_id` as `pivot_role_id` FROM `roles` inner join `account_role` on `roles`.`id` = `account_role`.`role_id` WHERE `account_role`.`account_id` in ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '60', '61', '62', '63', '64', '65', '67', '68', '70', '71', '72', '73', '74', '75', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100', '101', '102', '103', '104', '105', '106') 

das ist genau das, was wir wollen. Danke Taylor ;-)

+0

Es hat nicht für mich funktioniert. – brunoramonalmeida

Verwandte Themen