rtconner/laravel-likeable

I wrote this code, want to implement it?

dsbilling opened this issue · 3 comments

I use this code in my project to get a simple list of likers. Maybe you can implement this somehow?

Preview: http://pkmn.cards/card/BS-1

public function scopeGetLikers($query, $id) {
        $info = DB::table('likeable_likes')->where('likable_type', '=', 'Cards')->where('likable_id', '=', $id)->get();
        $totallikes = DB::table('likeable_like_counters')->where('likable_type', '=', 'Cards')->where('likable_id', '=', $id)->sum('count');

        if($info == null) {
            return 'No one';
        }

        $person = '';
        $comma = '';
        $int = 0;
        $limit = 2;
        $persons = array();

        foreach($info as $liker) {
            $persons[] = User::getUsernameByID($liker->user_id);
            $int++;
        }

        if($int < $limit) {
            $limit = $int;
        }

        $arrslice = array_slice($persons, $limit);
        $parr = array_slice($persons, 0, $limit);
        $miniperson = implode(', ', $parr);

        $others = $totallikes - $limit;

        if($int > $limit) {
            return $miniperson . ' and <a href="" id="likes" tabindex="0" role="button" data-toggle="popover" data-trigger="focus" data-content="'. implode(', ', $arrslice) . '">' . $others . ' other' . ($others == 1 ? '' : 's') . ' </a> ';
        }

        return $miniperson;

    }

Ok I'll look at it. Might be a few weeks until I have the time to look carefully at this.

Sounds good.

There was a bug in there. You can use likes attribute now.

@foreach($article->likes as $like)
   ...
@endforeach