如何在Laravel 4.2中制作HasMany Relation

时间:2015-11-21 08:01:13

标签: php mysql laravel-4.2

我的用户模型看起来像这样

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

    public function answer(){
        return $this->hasMany('Answer','user_id');
    }

    public function supplierranking(){
        return $this->hasMany('Supplierrank','userid','id');
    }
}

现在,每个用户都会在排名模型中对公司进行排名,该模型看起来像这样

Class Supplierrank extends Eloquent{
    public function supplier(){
        return $this->belongsTo('Supplier','supplierid','id');
    }

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

}

我能够向用户提供排名详细信息,但我还必须获取从供应商表中排名的公司的详细信息

看起来像这样

Class Supplier extends Eloquent{
    public function supplierranks(){
        return $this->hasMany('Supplierrank','supplierid');
    }

}

我所做的查询看起来像这样

$usersandranking = User::where('event','=',$exceleve)->with('supplierranking')->orderBy('id')->get();

它获取了我的用户详细信息和排名但不是供应商名称

任何人都可以帮助我吗

0 个答案:

没有答案