我是laravel的新人。我正在使用laravel 5.4开发新的Web应用程序。我想计算数据库中的数据数量并在视图中显示结果。所以,我在控制器中使用了这段代码:
public function bending_img(){
$counts = requestImg::where('is_done', '=', '0')->count();
return view('/dashboard')->with(['counts'=> $counts]);
}
public function uploaded_img(){
$count = requestImg::where('is_done', '=', '1')->count();
return view('/dashboard')->with(['count'=> $count]);
}
并在视野中:
@if(count($counts)== 0)
no call record to be viewed
@else
<div class="huge">{{$counts}}</div>
@endif
@if(count($count)==0)
no call record to be viewed
@else
<div class="huge">{{$count}}</div>
@endif
但有错误:undefined variable
。
请解决此错误。
答案 0 :(得分:0)
public function bendingImgCount() {
return requestImg::where('is_done', '0')->count();
}
public function uploadedImgCount() {
return = requestImg::where('is_done', '1')->count();
}
public function showDashboard() {
$bendingImgCount = $this->bendingImgCount();
$uploadedImgCount = $this->uploadedImgCount();
return view('/dashboard')->with(compact(['bendingImgCount', 'uploadedImgCount']));
}
调用showDashboard函数并在视图中将计数更改为bendingImgCount并计入uploadImgCount。
答案 1 :(得分:0)
首先,检查您在控制器处获得正确数据的位置 你只是
public function bending_img(){
$counts = requestImg::where('is_done', '=', '0')->count();
dd($counts);
return view('/dashboard')->with(['counts'=> $counts]);
}
public function uploaded_img(){
$count = requestImg::where('is_done', '=', '1')->count();
dd($count);
return view('/dashboard')->with(['count'=> $count]);
}
并检查你是否得到正确答案。
答案 2 :(得分:0)
答案 3 :(得分:0)
尝试这个:
在控制器中:
public function bending_img(){
$counts = requestImg::where('is_done', '=', '0')->count();
return view('/dashboard')->with('this_is_it', $counts);
}
public function uploaded_img(){
$count = requestImg::where('is_done', '=', '1')->count();
return view('/dashboard')->with('count_1',$count);
}
在视图中:
@if(count($this_is_it)== 0)
no call record to be viewed
@else
<div class="huge">{{$this_is_it}}</div>
@endif
@if(count($count_1)==0)
no call record to be viewed
@else
<div class="huge">{{$count_1}}</div>
@endif