File: D:/HostingSpaces/ZelfVerkopen/zelfverkopen.nl/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);
}
}