如何使Laravel属于关系根据条件

时间:2017-04-16 13:02:03

标签: php laravel eloquent

使用 Laravel 5.2 ,我尝试使用 belongsTo 方法从多个表中获取数据,并且应该根据条件,例如这在Eloquent模型中:

public function deals() {
    if($this->section=='offers') {
        return $this->belongsTo('App\Deals\SellOffer', 'record_id');
    } else if($this->section=='requests') {
        return $this->belongsTo('App\Deals\QuoteRequest', 'record_id');
    } else {
        return $this->belongsTo('App\Deals\Partnership', 'record_id');
    }
}

然后在控制器内部调用deal方法:

    $data = Options::with('deals')->get();

选项表有一个名为" section"的列,这应该引用3个表中的1个。

可以这样做,在运行代码之后,它总是在其他地方调用代码。

0 个答案:

没有答案
相关问题