Skip to content

Instantly share code, notes, and snippets.

@abfan1127
Created February 2, 2014 21:36
Show Gist options
  • Save abfan1127/8775304 to your computer and use it in GitHub Desktop.
Save abfan1127/8775304 to your computer and use it in GitHub Desktop.
Laravel Eloquent whereHas Escapes numeric breaking sqlite count(...) > ?
public function getFeaturedProducts_DOESNTWORK_WITH_SQLITE($storeId, $limit = 1)
{
$query = $this->product->newInstance()
->where('store_id', $storeId)
->limit($limit)
->with(
array(
'productMedia' => function ($query) {
/**
* we can't limit it because it gets all of the media at once
*/
$query->orderBy('mediaType','asc');
}
)
);
$query->whereHas('productAttribute',function ($query) {
return $query->where('attributeKey', 'featured')
->where('attributeValue', 'true');
},'>=',1); // <- this is an integer of 1.
$results = $query->get();
return $results;
}
public function getFeaturedProducts_WORKS_WITH_SQLLITE($storeId, $limit = 1)
{
$query = $this->product->newInstance()
->where('store_id', $storeId)
->limit($limit)
->with(
array(
'productMedia' => function ($query) {
/**
* we can't limit it because it gets all of the media at once
*/
$query->orderBy('mediaType','asc');
}
)
);
$query->whereHas('productAttribute',function ($query) {
return $query->where('attributeKey', 'featured')
->where('attributeValue', 'true');
},'>=', DB::raw('1')); // this works
$results = $query->get();
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment