warpig's avatar
Level 12

Method Illuminate\Database\Eloquent\Collection::whereDate does not exist.

I have a posts page where i display them all but I want to separate them, first show the latest then the rest of the posts that came before. Using query()->latest()->get() throws an error in the whereDate and this wasn't happening so what am i doing wrong here. thanks. error message https://flareapp.io/share/xmNr9vQm#F49

foreach loop with if statement on my view

        @foreach ($posts as $post)
            @if($loop->first())
                <article>
                    <div>
                        <img src="{{ $post->getThumbnail() }}">
                        <div>
                            <h3>
                                {{ $post->title }}</h3>
                            <p>
                                {{ $post->shortBody() }}</p>
                        </div>
                    </div>
                </article>
            @else
                <div>
                    <x-post-item :post="$post"></x-post-item>
                </div>
            @endif
        @endforeach
    public function index()
    {
        $posts = Post::query()->latest()->get()
        ->where('active', '=', 1)
        ->whereDate('published_at', '<', Carbon::now())
        ->orderBy('published_at', 'desc')
        ->paginate(3);
        return view('home', compact('posts'));
    }
0 likes
1 reply
Snapey's avatar

remove get(). You don't need that AND paginate()

get() terminates and executes the query. Everything from that point is a collection (which does not have a whereDate method)

Please or to participate in this conversation.