如何计算数据库行并在laravel 5.4视图中显示它?

时间:2017-04-30 15:20:59

标签: php database laravel laravel-5.4

我是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

请解决此错误。

4 个答案:

答案 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)

我在浏览器中获得正确计数

1.Controller Page:

enter image description here

2.web.php页面:

enter image description here

3.Web浏览器: enter image description here

如果您甚至没有获得正确的价值,那么只需显示您的整个页面代码。 谢谢。

答案 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