【发布时间】:2020-06-04 12:46:39
【问题描述】:
我有这两个模型。
我想在 youtube_video_insights 表中得到 12 列 comment_count 的总和。
如何使用 Query Builder 或 Eloquent 做到这一点。
我尝试在我的存储库中使用类似的东西,但它不起作用。
这只是所有 comment_count 列的总和。
$this->youtube_channel_insight
->join('youtube_video_insights',function ($q){
$q->on('youtube_channel_insights.id','=','youtube_video_insights.youtube_channel_insight_id')
->orderBy('youtube_video_insights.id','desc')
->limit(12);
})
->where('youtube_channel_insights.id',$id)
->select(\DB::raw(
"SUM(youtube_video_insights.comment_count) as total_comment"
))->get();
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class YoutubeChannelInsight extends Model
{
protected $guarded = ['id'];
public function videos()
{
return $this->hasMany(YoutubeVideoInsight::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class YoutubeVideoInsight extends Model
{
protected $guarded = ['id'];
}
【问题讨论】:
标签: php laravel eloquent laravel-query-builder