【发布时间】:2014-07-29 01:43:09
【问题描述】:
我有一个带有基本论坛系统的应用程序,用户可以多次“喜欢”一个主题。我的模型扩展了 Eloquent,我试图获得用户对特定主题的投票总和......基本上,我试图完成类似的事情:
$votes = Auth::user()
->votes->has('topic_id', '=', $topic->id)
->sum('votes');
但是,执行此操作时,我收到以下错误...
在非对象上调用成员函数 sum()
我也试过
public function show($forumSlug, $topicSlug)
{
$topic = Topic::whereSlug($topicSlug)->first();
$votes = Topic::whereHas('votes', function ($q) use ($topic)
{
$q->where('topic_id', '=', $topic->id)->sum('votes');
});
dd($votes);
}
但是,我收到一条错误消息:
“where 子句”中的未知列“ideas.id”(SQL:select sum(
votes) 作为来自votes的聚合,其中votes.idea_id=ideas.id和idea_id= 1)`
【问题讨论】:
标签: php laravel aggregate-functions eloquent relationship