获取数据我有一个deliveryForecast模型,我要创建一个雄辩得到基于2列从多个表中的数据。Laravel雄辩从多个相关表
这是我delivery_forecasts表
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();
});
的问题是我可以创建模型的口才?或如何使条件?
例如:
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
你需要创建'DeliveryForecast'模型为每个单独的关系内的多个功能的“多态”一节。 –