Reputation: 31
I want to count value numbers which type is equal to 0 using Laravel. Thanks.
controller
public function index()
{
$data['fundraise'] = Fundraised::get();
return view('fundraise',$data);
}
Html View
@if(count($fundraise) > 0)
@foreach ($fundraise as $value)
<li>
<a class="fundRaiserItem" href="">
<img src="{{config('markaz.file_url').$value->icon}}" alt="Icon">
<h3 class="fundRaisedCount">{{$value->number}}</h3>
<h5 class="fundRaisedTitle">{{$value->name}}</h5>
</a>
</li>
@php
$numbersValue = $value->where('type',0)->pluck('number');
$val = numbersValue->count();
dd($val);
@endphp
@endforeach
@endif
Upvotes: 0
Views: 307
Reputation: 1330
You called Fundraised::get()
which means get all Fundraised
and it's a collection
Note that $value
in @foreach ($fundraise as $value)
is single model object,
and it won't work if you try $value->where('type',0)->pluck('number')
because where
or pluck
methods are available on model collection, so simply try your code with calling where
and pluck
on $fundraise
variable. i.e:
@php
$val = $fundraise->where('type',$value->type)->count();
dd($val);
@endphp
As @shaedrich commented the best way is to pass all data at once to your blade and avoid using @php
tag in your blade
$data['fundraise'] = Fundraised::get();
$data['zero_count'] = $data['fundraise']->where('type', 0)->sum('number');
return view('fundraise',$data);
Upvotes: 0
Reputation: 5735
The eloquent collection is filterable and countable:
public function index()
{
$data['fundraise'] = Fundraised::get();
$data['zero_count'] = $data['fundraise']->where('type', 0)->sum('number');
return view('fundraise',$data);
}
Upvotes: 1
Reputation: 135
Step 1:
$data = DB::table('Your Table Name')->where('type','=',0)->get();
Step 2:
$countData = count($data);
Upvotes: 0