Reputation: 749
I've used auth scaffold to create login and auth system for my site. I want to count the post and show it in the dashboard. But in LoginController.php
and RegisterController.php
there is just one line code protected $redirectTo = '/home';
to return view. How do i count post and show it in the dashboard.
Upvotes: 0
Views: 907
Reputation: 749
Thanks to everyone who tried to help me out.
I found that when we use laravel auth scaffolding laravel uses use AuthenticatesUsers;
in LoginController.php
and use RegistersUsers;
in RegisterController.php
which is known as trait. And both trait use another trait RedeirectUsers.php
to redirect or show view after we login or register a new user. It would not be a nice idea messing up those files.But instead I used balde injection method in dashboard.blade.php
@inject('posts','App\Post') //place it in top of dashboard.blade.php
{{$posts->count()}}
No need to make extra view,models & controller to pass data to dashboard
Upvotes: 1
Reputation: 163768
If you want to count posts which are belong to the user, you can use count()
method:
$postsCount = Post::where('user_id', auth()->user()->id)->count;
Or using relationship:
$postsCount = auth()->user()->posts()->count();
Upvotes: 0