[英]How to count rows in database table?
I want to make a variable 我想做一个变量
{{ $countEvents }}
which can count the number of rows from table Events...Now I have 18 rows( https://imgur.com/a/zt35XwV ), I want to use this variable to my view. 它可以计算表事件中的行数...现在我有18行( https://imgur.com/a/zt35XwV ),我想在我的视图中使用此变量。 How can I be able to count the number of rows?
如何计算行数?
I've tried this 我已经试过了
$events = Event::count();
but I got all data with all columns from my database, not the number of it! 但是我从数据库中获得了所有列的所有数据,而不是数量!
Controller: 控制器:
$countEvents = Event::count();
return view('view-name-here', compact('countEvents'));
this will allow you to use {{ $countEvents }}
in your view 这将允许您在视图中使用
{{ $countEvents }}
You can use DB query builder facade. 您可以使用数据库查询构建器的facade。
$data['countEvents'] = DB::table('events')->count();
Now in blade you can check integer value of $countEvents
现在,您可以在刀片中检查
$countEvents
整数值
count
is a Collection method. count
是一个Collection方法。 The query builder returns an array. 查询构建器返回一个数组。 So in order to get the count, you would just count it like you normally would with an array:
因此,为了获得计数,您只需像通常使用数组那样对它进行计数:
$eventCount = count($events);
If you have a Event model, then you can use Eloquent to get a Collection and then use the Collection's count method. 如果您具有事件模型,则可以使用Eloquent来获取Collection,然后使用Collection的count方法。 Example:
例:
$eventlist = Event::all();
$eventCount = $eventlist->count();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.