如何在Laravel 4.2

时间:2015-11-23 05:55:13

标签: php laravel-4 laravel-4.2

我有一个supplierranking表如下

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

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

}

我正在尝试进行此查询

$usersandranking= Supplierrank::with('supplier')->with(array('user'=>function($query) use($exceleve){
        $query->where('event','=',$exceleve);
    }))->orderBy('userid')->orderBy('rank')->get();

在这里,您可以看到我正在尝试为所有用户获取supplierranking,其中event == execelevent

但查询是从用户表

中获取所有用户

我做错的任何想法

用户表看起来像这样

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

更新 使用后 DB :: enableQueryLog(); 和 DD(DB :: getQueryLog());

我得到了这个

array(5) { [0]=> array(3) { ["query"]=> string(44) "select * from `users` where `id` = ? limit 1" ["bindings"]=> array(1) { [0]=> int(71) } ["time"]=> float(0.59) } [1]=> array(3) { ["query"]=> string(39) "select * from `users` where `event` = ?" ["bindings"]=> array(1) { [0]=> string(32) "Design Mission South Africa 2015" } ["time"]=> float(3.41) } [2]=> array(3) { ["query"]=> string(63) "select * from `supplierranks` order by `userid` asc, `rank` asc" ["bindings"]=> array(0) { } ["time"]=> float(366.02) } [3]=> array(3) { ["query"]=> string(1051) "select * from `suppliers` where `suppliers`.`id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,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