Undefined variable error when I tried to display count in the dashboard page

Solution 1:

You have two routes with the same name going to two different methods in your controller. Laravel is only going to send the request to one of those routes. What this means is that only one of the methods is called to supply information back to the admin.admin-home view.

So, because you reference BOTH variables, $count and $pending on the admin-home view, and it is only getting one method called (thus supplying only $count and NOT $pending), the view looks for both variables, but it only has one. Thus, the error $pending is undefined -- it never gets to the applicantPending() method to supply the variable to the view.

To Fix, remove the bottom route from web.php. Then, combine the methods into one, and supply both variables back to the view.

Something like this:

public function applicantPending(){
        $pending=DB::table('applicants')
                -> where ('status', 'like', 'Pending')
                -> get();

       $count=DB::table('applicants')
                    -> where ('status', 'like', 'Pending')
                    -> count();

        return view('admin.admin-home', compact('pending', 'count'));
}