1
votes

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:&nbsp;&nbsp;{{ Auth::user()->posts->whereDate('created_at','=',$date)->sum('cijena') }}&euro;</th>
    <th>KARTICA:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'Kartica')->whereDate('created_at','=',$date)->sum('cijena')}}&euro;</th>
    <th>GOTOVINA:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'Gotovina')->whereDate('created_at','=',$date)->sum('cijena')}}&euro;</th>
    <th>VIRMAN:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'Virman')->whereDate('created_at','=',$date)->sum('cijena')}}&euro;</th>
    <th>NK:&nbsp;&nbsp;{{ Auth::user()->posts->where('placanje', 'NK')->whereDate('created_at','=',$date)->sum('cijena')}}&euro;</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?

1
The error is in your view you forgot brackets after each ->posts: you should use Auth::user()->posts()->... - dparoli
To get the user id, try Auth::user()->id - user6854465
Yes I forgot brackets.. That's what you get when panic.. Thank you dude. And I have another question if you can help me please, should I post a new question or we can do it here ? - Milos

1 Answers

2
votes

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')