Skip to content

Instantly share code, notes, and snippets.

@vuchkov
Forked from JeffreyWay/CommunityLinksQuery.php
Created February 8, 2019 13:24
Show Gist options
  • Save vuchkov/0708851083b484d47c677e961996f399 to your computer and use it in GitHub Desktop.
Save vuchkov/0708851083b484d47c677e961996f399 to your computer and use it in GitHub Desktop.

Revisions

  1. @JeffreyWay JeffreyWay revised this gist Aug 3, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CommunityLinksQuery.php
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ class CommunityLinksQuery
    * @param string|null $channel
    * @return \Illuminate\Database\Eloquent\Collection
    */
    public static function run($channel)
    public static function get($channel)
    {
    return CommunityLink::with('votes', 'creator', 'channel')
    ->forChannel($channel)
  2. @JeffreyWay JeffreyWay created this gist Aug 3, 2016.
    31 changes: 31 additions & 0 deletions CommunityLinksQuery.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    <?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 run($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);
    }
    }
    18 changes: 18 additions & 0 deletions LinksController.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?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'));
    }
    }