Laravel Count Row Query
December 19, 2024 ยท AI Conversations
User
laravel count row query
Assistant
To count the number of rows in a table in Laravel, you can use the `count()` method provided by Eloquent or the Query Builder. Here's how you can do it:
### Using Eloquent
If you have a model for your table, you can use:
$count = YourModel::count();
### Using Query Builder
If you prefer to use the Query Builder:
use IlluminateSupportFacadesDB;
$count = DB::table('your_table_name')->count();
### With Conditions
You can also add conditions before counting:
#### Eloquent Example:
$count = YourModel::where('status', 'active')->count();
#### Query Builder Example:
$count = DB::table('your_table_name')->where('status', 'active')->count();
### Example Output
echo "Total rows: " . $count;
Let me know if you need help with specific cases!