-
I'm not sure how silly question itself but: For example: class Order extends Model
{
public function products()
{
return $this->hasMany(OrdersProducts::class, 'order_id');
}
public function historicalProducts()
{
return $this->products()
->whereColumn('orders_products.created_at', '<', 'orders.created_at');
}
}
...
class OrdersProducts extends Model
{
public function order()
{
return $this->belongsTo(Order::class, 'order_id');
}
}
...
// Or without `historicalProducts` relation, just eager loading with some condition
Order::query()
->withWhereHas('products', fn ($query) => $query->whereColumn('orders.something', '=', 'products.something') If anyone had a similar problem and solved it, I would be very grateful! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
The relations being eager loaded you don't have for each relation the value of the parent column. |
Beta Was this translation helpful? Give feedback.
-
Wouldn't it be "product.order" as relationship key and then the column names as usual? |
Beta Was this translation helpful? Give feedback.
-
When listing operations, this is how an eager load relation query looks like:
Notice how operations table is not in the query. operation has a relations with client
client has
update Viceversa: Notice how clients are not in the query. |
Beta Was this translation helpful? Give feedback.
Yea, I know how it works under Laravel's hood, since I already spent a decent amount of time trying to understand "how to have "clients" in a query". Thanks for answering, I guess we can mark this thread as "resolved" considering that there's no way to achieve what I've initially sought for 🙏