HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/zelfverkopen.komma.pro/app/KommaApp/Posts/LatestPostsComposer.php
<?php


namespace App\KommaApp\Posts;

use Carbon\Carbon;
use Illuminate\View\View;

class LatestPostsComposer
{

    static $posts;
    private $today;

    public function __construct()
    {
        $this->today = Carbon::now()->addHour();
        $this->today = $this->today->format('Y-m-d H:i:s');
    }

    /**
     * Bind data to the view.
     *
     * @param  View $view
     * @return void
     */
    public function compose(View $view)
    {

        // Only load posts once
        if (!static::$posts) {

            if(!empty(\App::getSite())) {

                // Load the latest 4 for
                static::$posts = \App::getSite()
                    ->posts()
                    ->with('translation', 'images', 'author', 'author.images', 'post_categories',
                        'post_categories.translation')
                    ->where('active', 1)
                    ->where('date', '<=', $this->today)
                    ->orderBy('date', 'desc')
                    ->orderBy('created_at', 'desc')
                    ->take(5)
                    ->get();

            }
        }

        if(empty(\App::getSite())) {
            $view->with('posts', collect());
            return;
        }

        // Footer view only need 3 posts
        if($view->getName() === 'site.partials.footer') $posts = static::$posts->take(3);
        // And post row only needs 4
        elseif($view->getName() === 'site.partials.postsRow') $posts = static::$posts->take(4);
        else $posts = static::$posts;

        $view->with('posts', $posts);

    }

}