php - Laravel Count with where inside blade -
i'm trying total comments user have..
controller:
public function index() { $setting = setting::findorfail(1); $comments = comment::where('published', '=', '1')->get(); $users = user::all(); return view('page.index', compact('setting', 'comments', 'users')); }
view:
@foreach($comments $comment) {{ count($users->where('user_id', '=', $comment->user_id)) }} @endforeach
the problem returns 0 , have 2 comments there.. using user id instead of "$comment->user_id" doesnt work. still display 0.
$users
collection, not query. treat such:
@foreach ($comments $comment) {{ $users->where('user_id', $comment->user_id)->count() }} @endforeach
the collection's where
method not take operator.
from wording in question seems want other way around:
$comments = comment::wherepublished(1)->get()->groupby('user_id');
then in view:
@foreach ($users $user) {{ $comments->has($user->id) ? count($comments[$user->id]) : 0 }} @endforeach
Comments
Post a Comment