I have two tables :
customer table =
id,
score,
total_buy,
(this table has many transaction)
And have a transaction table too
customer_id,
value,
score,
created_at,
(this table blongs to a customer)
i want Search on customer where has 5 transaction and one of them created at 2017/1/1 date and where customer score and value more than 200 and take 10 row By Random order (its a lottery system ) i Trying this code before ask
customer::
whereHas('transactions', function ($query) use ($last_transaction)
{
$query->whereDate('created_at',$last_transaction);
})
->where('total_buy', '>=',$r->minvalue)
->where('score', '>=',$r->score)
->inRandomOrder()
->take($r->customernumber)
->get();
but i dont know how counting Row subrelation in where has
Use havingRaw()
.
customer::
whereHas('transactions', function ($query) use ($last_transaction)
{
$query->havingRaw('COUNT(*) > 4');
})->
whereHas('transactions', function ($query) use ($last_transaction)
{
$query->whereDate('created_at',$last_transaction);
})
->where('total_buy', '>=',$r->minvalue)
->where('score', '>=',$r->score)
->inRandomOrder()
->take($r->customernumber)
->get();
This will check for atleast 5 transactions
. If you need exact
5, then use equal
operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With