4
4
4sadly2021-10-17 17:20:58
Laravel
4sadly, 2021-10-17 17:20:58

How to build a query correctly?

I need to find a model1 that has a model2, where model2 has a number greater than the number of model 1 by 10
, I try like this:

Model1::whereHas('model2', function ($query){
    $query->where('number',  '>',   $model1->number+10)
});

How to use $model1 in the wherehas function?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kovalchuk, 2021-10-17
@4sadly

You need to do it through mysql because at the query level you don't have $model1
something like this

Model1::whereHas('model2', function ($query){
    $query->whereRaw('number',  '>',   'model1.number+10')
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question