z3r0
z3r0

Reputation: 139

Query Laravel Relationship

I have 3 Model

In Purchase Model

    protected $fillable = [
        'name', 'date', 'description', 'active', 'supplier', 'total', 'paid', 'purchase_status', 'payment_status',
    ];

    public function product()
    {
        return $this->belongsTo(\App\Product::class);
    }

    public function order()
    {
        return $this->belongsTo(\App\Order::class);
    }

In Order Model

protected $fillable = [
        'name', 'quantity', 'unit_cost', 'discount', 'tax', 'sub_total'
    ];

    public function order_items()
    {
        return $this->belongsTo(\App\OrderItem::class);
    }

In my OrderItem Model

    protected $fillable = ['active', 'description', 'quantity', 'discount', 'unit_price'];

    public function order()
    {
        return $this->belongsTo(\App\Order::class);
    }

Is it possible to query Order_Items From Purchases trough Orders Relationship in Laravel?

Upvotes: 0

Views: 107

Answers (3)

Cvetan Mihaylov
Cvetan Mihaylov

Reputation: 950

I see that you've fixed the relationship Order -> OrderItem of belongsTo to hasMany. But the question was also about how to query through relationships so I'll just add up to this so it could be useful to other users here.

Now you could do

$order_items = $purchase->order->order_items

to get the order items of the purchase.

However you should consider that this would execute further queries to the DB. One for fetching the order and then another one for fetching the order_items. If you're looping through a list of purchases, this could escalate quickly and end up making too much DB queries and affect the performance of your application.

The solution is eager loading.

You could do either pre-fetch the order and items along with the purchase like this:

$purchase = Purchase::with('order.order_items')->find(1);

or if you've already fetched the purchase, then you could do:

$purchase->load('order.order_items');

Then when getting the order items in your code like this $purchase->order->order_items, you have no additional queries to the DB.

Upvotes: 0

Farid shahidi
Farid shahidi

Reputation: 352

Yes you can write this relations in your models and write the query like this:

Purchase

public function order()
{
    return $this->belongsTo('App\Models\Order', 'order_id', 'id');
}

Order_Item

public function order()
{
    return $this->belongsTo('App\Models\Order', 'order_id', 'id');
}

Order

public function order_items()
{
    return $this->hasMany('App\Models\OrderItem', 'order_id', 'id');
}

public function purchases()
{
    return $this->hasMany('App\Models\purchase', 'order_id', 'id');
}

Now you can use this query:

$purchase = \App\Models\Purchase::first();
$order_items = $purchase->order->order_items;

Upvotes: 0

z3r0
z3r0

Reputation: 139

Oh, i found my answer...

In Order Model

i changed belongsTo to hasMany

   protected $fillable = [
        'name', 'quantity', 'unit_cost', 'discount', 'tax', 'sub_total'
    ];

    public function order_items()
    {
        return $this->hasMany(\App\OrderItem::class);
    }

Upvotes: 1

Related Questions