Last active
August 29, 2015 13:55
-
-
Save msurguy/8747269 to your computer and use it in GitHub Desktop.
Get 3 random items from a category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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