i have a deliveryForecast model and i have to create an eloquent to get data from multiple table based on 2 columns.
this is my delivery_forecasts table
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();
});
the question is can i create the eloquent in model? or how to make condition?
for example:
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 .....
}
}
i don't have idea to create the condition to eloquent and my query should be like this:
$delivery_forecast = DeliveryForecast::with('documents')
->get();
any idea guys? thanks in advance.
DeliveryForecastmodel for each individual relationship.