Skip to content
Advertisement

Laravel – Filtering a Model by Condition between “a Field and Its Relationship Model Field”

I have these two models with one to one relationship.

“products”

  • id
  • name
  • minimum_required

“product_data”

  • id
  • product_id
  • price
  • oh_hand

I want to get the count of product_data where its on_hand is less than its related product’s minimum_required.

I’ve tried subqueries and I still can’t figure it out. The query I want may looks something like this.

JavaScript

Advertisement

Answer

you can join the tables then use ‘whereColumn’:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement