I am trying to fetch random number of rows from table in laravel 5.7, but i could not find any solution. I have use
Model::all()->random(2);
It work fine. But i need to apply where clause with it like Model::select('column')->where('column','value')->random(number of rows');
So how can i achieve this using eloquent.
Please any suggestions for me.
You can simply add to chain inRandomOrder
, as suggested here:
Laravel - Eloquent or Fluent random row
And then limit your dataset.
Model::select('column')
->where('column','value')
->inRandomOrder()
->limit(2) // here is yours limit
->get();
You could use the inRandomOrder
method in combination with first
, like this: Model::inRandomOrder()->select('column')->where('column','value')->first();
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