Laravel 4范围不工作的确切金额
问题描述:
所以我的问题是,当“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;
}
答
固定它自己! :)
所以这里是解决方案:
- 删除范围“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; }