Reputation: 27
I'm new to Laravel and i need your help please.
This runs on my controller and the result should be "3" as long as I have 3 rows with that title in the same table.
$countcategories = DB::table('categories')
->select(DB::raw('count(title) as result'))
->where('title','=','dsds')
->get();
I'm passing the query result to the view like this->with('counts', $countcategories)
And now my questions are:
foreach
. Why? My query returns only one row.Thank you very much!
Upvotes: 0
Views: 2336
Reputation: 71
you should use ->first() method:
$countcategories = DB::table('categories')
->select(DB::raw('count(title) as result'))
->where('title','=','dsds')
->first();
Upvotes: 2
Reputation: 10394
get
ting are rows from a result, it tunrs out there is only one in your situation.Anyways, here's my approach:
// The result
$categories = Category::where('title','=','dsds');
foreach( $categories->all() as $category ) {
echo $category->title . "\n";
}
// The count
echo $categories->count();
Upvotes: 0