Reputation: 47
I try to display count for pending applicants in my dashboard page. However, it shows error like below.
the undefined variable $pending
is my another function called applicantPending() to display the info of pending applicants and it run smoothly. It is also shown in my dashboard page.
However after I try to display count in the other function called applicantTotal() , it is undefined.
The functions in ApplicantController;
public function applicantPending()
{
$pending = DB::table('applicants')
->where ('status', 'like', 'Pending')
->get();
return view('admin.admin-home', ["pending" => $pending]);
}
//to display count
public function applicantTotal()
{
$count = DB::table('applicants')
->where ('status', 'like', 'Pending')
->count();
return view('admin.admin-home', compact('count'));
}
web.php
Route::get('index', [ApplicantController::class, 'applicantPending']);
Route::get('index', [ApplicantController::class, 'applicantTotal']);
To display count in the admin-home.blade.php (dashboard),
<div class="h5 mb-0 mr-3 font-weight-bold text-gray-800">{{$count}}</div>
To display info' of each applicant in the admin-home.blade.php (dashboard).
@foreach ($pending as $item)
<table class="table">
<tbody>
<tr>
<td>{{$item->nama}}</td>
<td>{{$item->noPhone}}</td>
<td>{{$item->email}}</td>
<td style="color: crimson">{{$item->status}} </td>
</tr>
</tbody>
</table>
@endforeach
I think there's something wrong in my web.php
.
Upvotes: 0
Views: 721
Reputation: 8178
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'));
}
Upvotes: 4