Laravel 4范围不适用于确切金额

So my problem is that when the limit in "getAllLikers" is hit it stops and gives me an "Object of class Illuminate\Database\Eloquent\Builder could not be converted to string" error in Laravel 4.2.11. I am using the Likable plugin by rtconner (https://github.com/rtconner/laravel-likeable). It works fine when the amount of likers is below or over the limit, but when it is the exact amount it won't work.

I have tried a lot of different things to try to work around it but I can't seem to make it work. I have also got a friend to look at it and he did not either find a solution.

Do any of you have any suggestions for me? See code below:

Blade Template:

<h3 class="text-muted"><small>{{ Model::getLikers($id) }} like this.</small></h3>

Model:

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

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

    $person = '';
    $comma = '';
    $int = 0;
    $limit = 1;

    foreach($info as $liker) {

        if($int == 0) {
            $comma = '';
        } else {
            $comma = ', ';
        }

        if($int <= $limit) {
            $person = $person . $comma . User::getUsernameByID($liker->user_id);
        }

        $int++;

    }

    if($int > $limit) {
        return $person . ' and <a href="" id="likes" tabindex="0" role="button" data-toggle="popover" data-trigger="focus" data-content="'. Model::getAllLikers($id) . '">' . ($totallikes - $limit - 1) . ' others </a> ';
    }

    return $person;
}

public function scopeGetAllLikers($query, $id) {
    $info = DB::table('likeable_likes')->where('likable_type', '=', 'Model')->where('likable_id', '=', $id)->get();

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

    $person = '';
    $comma = '';
    $int = 0;
    $limit = 2;

    foreach($info as $liker) {

        if($int = $limit) {
            $comma = '';
        } else {
            $comma = ', ';
        }

        if($int > $limit) {
            $person = $person . $comma . User::getUsernameByID($liker->user_id);
        }

        $int++;

    }

    return $person;
}

Fixed it myself! :)

So here is the solution:

  • Removed the scope "GetAllLikers" because this is where everything went wrong.
  • Rewrote the "GetLikers" scope, see code below.

    public function scopeGetLikers($query, $id) {
    $info = DB::table('likeable_likes')->where('likable_type', '=', 'Model')->where('likable_id', '=', $id)->get();
    $totallikes = DB::table('likeable_like_counters')->where('likable_type', '=', 'Model')->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;
    
    }