Created
August 3, 2016 20:12
-
-
Save JeffreyWay/9c7071aee31c1b2db3ec595d6d3373b7 to your computer and use it in GitHub Desktop.
Consider Query Objects source.
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
<?php | |
namespace App\Queries; | |
use App\CommunityLink; | |
class CommunityLinksQuery | |
{ | |
/** | |
* Fetch all relevant community links. | |
* | |
* @param string|null $channel | |
* @return \Illuminate\Database\Eloquent\Collection | |
*/ | |
public static function get($channel) | |
{ | |
return CommunityLink::with('votes', 'creator', 'channel') | |
->forChannel($channel) | |
->where('approved', 1) | |
->leftJoin( | |
'community_links_votes', | |
'community_links_votes.community_link_id', '=', 'community_links.id' | |
) | |
->selectRaw( | |
'community_links.*, count(community_links_votes.id) as vote_count' | |
) | |
->groupBy('community_links.id') | |
->orderBy('updated_at', 'desc') | |
->paginate(3); | |
} | |
} |
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
<?php | |
namespace App\Http\Controllers; | |
use App\Queries\CommunityLinksQuery; | |
class LinksController extends Controller | |
{ | |
/** | |
* Display all community links. | |
*/ | |
public function index() | |
{ | |
$links = CommunityLinksQuery::get(); | |
return view('community.links', compact('links')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment