我有一個基本論壇系統的應用程序,用戶可以多次「喜歡」一個主題。我的模型延伸口才,我想拿到票的用戶具有特定主題的總和......基本上,我試圖完成這樣的:在Laravel的集合上執行sum()
$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子句'(SQL
未知列 'ideas.id':SELECT SUM(
votes
) 作爲骨料從votes
其中votes
。idea_id
=ideas
。id
和idea_id
= 1)`
你可以在這裏看到我的關係:http://www.laravelsd.com/share/8jQRWl – user1960364