如何在 Laravel 5 中显示计数数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39241732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show Count Data in Laravel 5
提问by Widyan Fakhrul Arifin
Im new in Laravel, 5 hours i have tried to adding this function and im so confused.
Case :I want to show count data in table tb_surat
in dashboard (i used sximo)
ex, if in tb_surat
i have 12 rowdata. how to show this like
"Total data : 12"
我是 Laravel 的新手,5 个小时后我尝试添加此功能,但我很困惑。
案例:tb_surat
如果tb_surat
我有12 行数据,
我想在仪表板的表中显示计数数据(我使用 sximo)。如何显示
“总数据:12”
help me please, give me step by step to adding in controller or view or route or etc, i dont understand. thanks.
请帮助我,给我一步一步地添加控制器或视图或路由等,我不明白。谢谢。
回答by whoan
You can use the query builder in this way:
您可以通过以下方式使用查询构建器:
DB::table('tb_surat')->count()
回答by Sulthan Allaudeen
I would strongly recommend you to use Eloquent way. It is as simple as below
我强烈建议您使用 Eloquent 方式。就像下面一样简单
Step 1 :
第1步 :
Create a Model
创建模型
php artisan make:model Surat
php artisan make:model Surat
Step 2 :
第2步 :
Inside the Model name the table by
在模型中命名表
protected $table = 'tb_surat';
protected $table = 'tb_surat';
then simply
然后简单地
Surat::count();
Surat::count();
Then as if you need to pass the result in your view simply pass it from the controller like
然后好像你需要在你的视图中传递结果一样简单地从控制器传递它
$count = Surat::count();
return View::make('index')->with('count', $count);
Where your view will have
你的观点会在哪里
Total data : {{$count}}
Total data : {{$count}}
Note :
笔记 :
The reason why i recommend you to use model is to follow the Eloquent way as it was the best practise ever.
It would be better if you have a fresh start with the Laravel Documentation
我推荐你使用模型的原因是遵循 Eloquent 的方式,因为它是有史以来的最佳实践。
如果您重新开始使用Laravel 文档会更好