Php Laravel雄辩地从多个相关表中获取数据

Php Laravel雄辩地从多个相关表中获取数据,php,mysql,laravel,eloquent,laravel-5.2,Php,Mysql,Laravel,Eloquent,Laravel 5.2,我有一个deliveryForecast模型,我必须创建一个有说服力的模型,以便从基于2列的多个表中获取数据 这是我的交货预测表 Schema::create('delivery_forecasts', function (Blueprint $table) { $table->increments('id'); $table->enum('type',array('Quotation','Demo','Service Unit','Freebie',

我有一个deliveryForecast模型,我必须创建一个有说服力的模型,以便从基于2列的多个表中获取数据

这是我的交货预测表

Schema::create('delivery_forecasts', function (Blueprint $table) {
        $table->increments('id');
        $table->enum('type',array('Quotation','Demo','Service Unit','Freebie','Back Job','Site Visit'));
        $table->string('type_id');
        $table->date('transaction_date');
        $table->time('transaction_time')->nullable();
        $table->enum('status',array('Pending','Approved','Cancelled'))->default('Pending');
        $table->boolean('queue')->default(0);
        $table->timestamps();
    });
问题是我能创造出雄辩的in模型吗?或者如何创造条件

例如:

class DeliveryForecast extends Model
{
    public function documents(){
        if(DeliveryForecast->type == 'Quotation'){
            return $this->belongsTo('App\Quotation','type_id','id');
        }
        if(DeliveryForecast->type == 'Demo'){
            return $this->belongsTo('App\Demo','type_id','id');
        }
        if(DeliveryForecast->type == 'Service Unit'){
            return $this->belongsTo('App\ServiceUnit','type_id','id');
        }
        and so on .....
    }
}
我不想为雄辩创造条件,我的问题应该是这样的:

$delivery_forecast = DeliveryForecast::with('documents')
        ->get();

有什么想法吗,伙计们?提前感谢。

正如@Scopey所说,看看多态关系:

为了实现多态关系,您必须将迁移更改为以下内容:

    Schema::create('delivery_forecasts', function (Blueprint $table) {
        $table->increments('id');
        $table->morphs('type');
        $table->date('transaction_date');
        $table->time('transaction_time')->nullable();
        $table->enum('status', ['Pending', 'Approved', 'Cancelled'])->default('Pending');
        $table->boolean('queue')->default(0);
        $table->timestamps();
    });
public function document()
{
    return $this->morphTo('type');
}
然后将
DeliveryForecast
的方法更改为:

    Schema::create('delivery_forecasts', function (Blueprint $table) {
        $table->increments('id');
        $table->morphs('type');
        $table->date('transaction_date');
        $table->time('transaction_time')->nullable();
        $table->enum('status', ['Pending', 'Approved', 'Cancelled'])->default('Pending');
        $table->boolean('queue')->default(0);
        $table->timestamps();
    });
public function document()
{
    return $this->morphTo('type');
}
就这些。但我强烈建议在您的
报价
演示
和其他模型中添加关系:

public function deliveryForecasts()
{
    return $this->morphMany('App\DeliveryForecast', 'type');
}

查询
$forecast->document
Laravel将自动为获取正确的模型,无需任何其他条件子句。

查看“多态性”在您的场景中,您需要在
DeliveryForecast
模型中为每个单独的关系创建多个功能。感谢@Scopey和@Giedrius Kiršys,这与我的预期一样完美。最后一件事,是否有一种方法可以在多态雄辩中添加另一个关系?像这样的<代码>返回$this->morphMany('App\DeliveryForecast','type')->带('customer')因为deliveryForecast中的所有相关表(如('Quotence'、'Demo'、'Service Unit')都有自己的customer\u id列。