【问题标题】:Laravel Eloquent hasManyThrough Relationship, SELECT withoutLaravel Eloquent hasManyThrough 关系,SELECT 没有
【发布时间】:2015-07-27 20:30:20
【问题描述】:

我有 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');

    }    

}

【问题讨论】:

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


    【解决方案1】:

    没有记录,但 Company::doesntHave('tickets') 应该可以工作。

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

    【讨论】:

    • 这种东西应该在主页上:p
    猜你喜欢
    • 2020-10-14
    • 2016-09-26
    • 2019-03-26
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 2018-10-18
    相关资源
    最近更新 更多