2014-11-02 81 views
0

所以我的問題是,當「getAllLikers」的限制被擊中時,它會停止並給我一個「照明\數據庫\ Eloquent \ Builder的對象無法轉換爲字符串」錯誤在Laravel 4.2.11中。我通過rtconner使用Likable插件(https://github.com/rtconner/laravel-likeable)。當相似者的數量低於或超過限制時,它可以正常工作,但是當它是確切的數量時,它將不起作用。Laravel 4範圍不工作的確切金額

我嘗試了很多不同的事情來嘗試解決它,但我似乎無法讓它工作。我也有一個朋友來看看它,但他沒有找到解決方案。

對我有沒有任何建議?請參見下面的代碼:

刀片模板:

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

型號:

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; 
} 

回答

0

固定它自己! :)

所以這裏是解決方案:

  • 刪除範圍「GetAllLikers」,因爲這是一切都錯了。
  • 重寫了「GetLikers」作用域,請參閱下面的代碼。

    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; 
    
    }