Support the ongoing development of Laravel.io →
posted 11 years ago
Database

Hello,

My query is returning the wrong product.

My Order.php has a relation of public function items() { return $this->hasMany('item','order_id'); }

My Item.php has a relation of public function order() { return $this->belongsTo('Order','order_id'); }

public function product()
{
    return $this->hasOne('product','id','product_id');
}

My query is $model = Order::where('id','=',$id)->has('items')->first();

Last updated 3 years ago.
0

Nobody has any ideas why I get from wrong product when I query the Orders.Items and reference the product relation?

Last updated 3 years ago.
0

I just thought I would let everyone know that I had the relation defined wrong. Changed hasOne to belongsTo and the correct product is now showing.

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

frocco frocco Joined 3 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.