Reputation: 602
So in this project I have homepage which is showing only posts that are made that day. And right now I need my price summing for that day and for that I add this to my price summing code
In my home.blade.php
for summing price :
<tfoot>
<tr>
<th>UKUPAN IZNOS: {{ Auth::user()->posts->whereDate('created_at','=',$date)->sum('cijena') }}€</th>
<th>KARTICA: {{ Auth::user()->posts->where('placanje', 'Kartica')->whereDate('created_at','=',$date)->sum('cijena')}}€</th>
<th>GOTOVINA: {{ Auth::user()->posts->where('placanje', 'Gotovina')->whereDate('created_at','=',$date)->sum('cijena')}}€</th>
<th>VIRMAN: {{ Auth::user()->posts->where('placanje', 'Virman')->whereDate('created_at','=',$date)->sum('cijena')}}€</th>
<th>NK: {{ Auth::user()->posts->where('placanje', 'NK')->whereDate('created_at','=',$date)->sum('cijena')}}€</th>
</tr>
</tfoot>
And this whereDate
method is from my HomeController
and here it is:
public function index()
{
$date = new Carbon(request('date'));
$posts = Post::where('user_id', Auth::id())
->whereDate('created_at','=',$date)
->orderBy('created_at', 'DESC')
->paginate(30); //add {{ $posts->links() }} if paginate is enabled
return view('home', compact('date', $date))->with('posts', $posts);
}
And my route in web.php
is:
Route::get('/home', 'HomeController@index')->name('home');
And all it returns me is that method whereDate does not exist. Any ideas how to fix this?
Upvotes: 1
Views: 1605
Reputation: 9161
In your view you use some lines with:
Auth::user()->posts->where...
Consider that Auth::user()->posts
returns a collection
of user posts but Auth::user()->posts()
returns a query builder
instance.
Luckly laravel collection
has the where()
method defined but it has not the whereDate()
method, instead this is defined on the query builder
, so in your view you have to use this line:
Auth::user()->posts()->whereDate('created_at','=',$date)->sum('cijena')
Upvotes: 2