1

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.

2
  • 1
    Have a look at the "polymorphic" section of the Eloquent documentation Commented Nov 10, 2016 at 4:05
  • in your scenario you need to create multiple function inside the DeliveryForecast model for each individual relationship. Commented Nov 10, 2016 at 6:11

1 Answer 1

3

As @Scopey said, take a look at polymorphic relations: https://laravel.com/docs/5.2/eloquent-relationships#polymorphic-relations

In order to implement polymorphic relationship You have to change Your migration to following:

    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();
    });

and then change DeliveryForecast's method to following:

public function document()
{
    return $this->morphTo('type');
}

And that's all. But I would strongly suggest to add relationship in Your Quotation, Demo and other models:

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

When querying $forecast->document Laravel will automatically fetch correct model for without any other conditional clauses.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks @Scopey and @Giedrius Kiršys, this works perfectly as i expected. last thing, is there's a way i can add another relation in polymorphic eloquent? something like this... return $this->morphMany('App\DeliveryForecast', 'type')->with('customer'); because all of related tables in deliveryForecast like ('Quotation','Demo','Service Unit') has its own customer_id column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.