Mar 16, 2015
0
Level 14
Gotchas
I hope this takes off. If it does, I'll be checking it daily and adding to it as I discover new ones.
What am I talking about? I'm suggesting that we post the GOTCHAS we come across, possibly saving each other tons of time.
Here's my first:
SQLITE uses RANDOM(), and MySQL uses RAND().
While testing a controller method (using an SQLITE database) instead of the production (MySQL) database, I just couldn't figure why the following wasn't working:
$results = Product::where('store_id', '=', $store_id)
->orderByRaw("RAND()")
->get(['id', 'brand_id']);
My way around it was checking the APP_ENV value:
// sqlite uses RANDOM instead of RAND!!!!
if (env('APP_ENV') == 'testing')
{
// get some
$results = Product::where('store_id', '=', $store_id)
->orderByRaw("RANDOM()")
->get(['id', 'brand_id']);
}
else {
// get some
$results = Product::where('store_id', '=', $store_id)
->orderByRaw("RAND()")
->get(['id', 'brand_id']);
}
Please or to participate in this conversation.