Skip to content

Instantly share code, notes, and snippets.

@msurguy
Last active August 29, 2015 13:55
Show Gist options
  • Save msurguy/8747269 to your computer and use it in GitHub Desktop.
Save msurguy/8747269 to your computer and use it in GitHub Desktop.
Get 3 random items from a category
$site = Site::approved()->featured()->where('slug',$slug)->first();
$related = array();
if(count($site->categories) > 0){
$categoryIDs = array();
foreach ($site->categories as $category) {
$categoryIDs[] = $category->id ;
}
shuffle($categoryIDs);
$relatedCategory = Category::with(array('sites' => function($query)
{
$query->approved()->featured()->orderBy('created_at','desc');
}))->find($categoryIDs[0]);
$related = $relatedCategory->sites()->approved()->featured()->whereNotIn('site_id', array($site->id))->orderBy(DB::raw('RAND()'))->take(3)->remember(60)->get();
}
return View::make('sites.view', array('site'=>$site, 'related' => $related));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment