Milos
Milos

Reputation: 602

Laravel - method whereDate does not exist

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?

Upvotes: 1

Views: 1605

Answers (1)

dparoli
dparoli

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

Related Questions