Skip to content
Advertisement

Compare two columns from two related tables using scopes in Laravel 9

I’m trying to create a scope that will compare two columns on two related tables.

Based on these tables I would like to be able to get all instances of the ServiceCall model where the next_service_date is within the next 15 days, where the Customer model either has a null value for the last_contact_date or where it’s before the ServiceCall‘s next_service_date.

Relevant table structure:

customers

  • id
  • last_contact_date

service_calls

  • id
  • customer_id
  • next_service_date

Working SQL for what I’m trying to accomplish:

JavaScript

Is there a way to accomplish this part of the SQL customers.last_contact_date < service_calls.next_service_date with scopes?

This is what I have so far which does everything except the above.

Customer model:

JavaScript

ServiceCall model:

JavaScript

Thanks!

Advertisement

Answer

I was able to solve this using an additional package kirschbaum-development/eloquent-power-joins

I removed the scope from the Customer model

ServiceCall model:

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