Reputation: 251
I want to make a variable
{{ $countEvents }}
which can count the number of rows from table Events...Now I have 18 rows( https://i.sstatic.net/ZUJoY.jpg ), I want to use this variable to my view. 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!
Upvotes: 1
Views: 227
Reputation: 18577
You can use DB query builder facade.
$data['countEvents'] = DB::table('events')->count();
Now in blade you can check integer value of $countEvents
Upvotes: 1
Reputation: 21
count
is a Collection method. 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. Example:
$eventlist = Event::all();
$eventCount = $eventlist->count();
Upvotes: 1
Reputation: 2267
Controller:
$countEvents = Event::count();
return view('view-name-here', compact('countEvents'));
this will allow you to use {{ $countEvents }}
in your view
Upvotes: 3