I'm trying to get the link karma (like reddit does) of a user and display it on his profile page.
user: id, name, email, password..
posts: id, title, link, text...
votes: id, user_id, post_id...
I need to be able to get the sum of "value" field in "votes" table of all the posts submitted by the user
This is what I'm using now and a quick dd($user)
will retrieve all the post and votes by the user. But using {{ $post->votes->sum('value') }}
on the view will retrieve the sum of each post separately and not all of them together. How can I do that?
public function show($user)
{
$user = User::whereName($user)->with('posts.votes')->first();
return view('user/profile')->with('user', $user);
}
Note that dd($user)
does retrieve all the posts and their votes of the user.
The View (which displays as many times as there as posts related to the user which is not what should be happening)
@foreach($user->posts as $post)
Link Karma: {{ $post->votes->sum('value') }}
@endforeach
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire