Laravel Eloquent拥有很多关系,SELECT没有

时间:2015-05-15 19:43:58

标签: laravel-4 eloquent has-many-through

我有3个实体:

公司,用户,票证

我需要选择所有没有门票的公司,我如何使用Eloquent来管理?

这是关系

class Company extends Eloquent {

    protected $table = 'companies';

    protected $fillable = array('name', 'priority', 'shortname', 'color');

    public function users() {

        return $this->hasMany('User', 'companies_id');

    }


    public function tickets() {

        return $this->hasManyThrough('Ticket', 'User', 'companies_id', 'from_users_id');

    }

}

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

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

    public function company() {

        return $this->belongsTo('Company', 'companies_id');

    }

    public function tickets() {

        return $this->hasMany('Ticket', 'from_users_id');

    }

}

class Ticket extends Eloquent {

    protected $table = 'tickets';

    public function user() {

        return $this->belongsTo('User', 'from_users_id');

    }    

}

1 个答案:

答案 0 :(得分:3)

没有记录,但Company::doesntHave('tickets')应该有效。

http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Builder.html#method_doesntHave