Skip to content
This repository has been archived by the owner on Sep 5, 2020. It is now read-only.

feat(Social Posts): move post loading logic into view composer #61

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/Http/ViewComposers/SocialPostsComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\ViewComposers;

use App\SocialPost;
use App\InstagramPost;
use Illuminate\Contracts\View\View;

class SocialPostsComposer
{
protected $socialPosts;
protected $instagramPosts;

public function __construct(SocialPost $socialPosts, InstagramPost $instagramPosts)
{
$this->socialPosts = $socialPosts;
$this->instagramPosts = $instagramPosts;
}

public function compose(View $view)
{
$view->with('twitterPosts', $this->socialPosts->recent())->with('instagramPosts', $this->instagramPosts->recent());
}
}
33 changes: 33 additions & 0 deletions app/InstagramPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class InstagramPost extends SocialPost
{
protected $table = 'social_posts';

public static function boot()
{
parent::boot();

static::addGlobalScope(function ($query) {
$query->where('platform', 'instagram');
});
}

public function recent($limit=3) {
$posts = $this->orderBy('pubDate','desc')->limit($limit)->get();

$posts = $posts->map(function ($item) {
return [
'srcLarge' => $item->media,
'srcSmall' => $item->media,
'socialUrl' => $item->socialUrl,
];
});

return $posts;
}
}
24 changes: 0 additions & 24 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use App\SocialPost;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -25,28 +24,5 @@ public function register()
*/
public function boot()
{
//
//footer variables
//tweets & facebook posts
$twitterArray = SocialPost::whereIn('platform',['twitter','facebook'])->orderBy('pubDate','desc')->limit(3)->get();
$twitterArray = $twitterArray->map(function ($item) {
return [
'source' => $item->platform,
'data' => $item->text,
'url' => $item->socialUrl,
'time' => $item->pubDate,
];
});

//instagram
$instagramArray = SocialPost::where('platform','instagram')->orderBy('pubDate','desc')->limit(8)->get();
$instagramArray = $instagramArray->map(function ($item) {
return [
'srcLarge' => $item->media,
'srcSmall' => $item->media,
];
});

View::share(['instagramArray' => $instagramArray, 'twitterArray' => $twitterArray]);
}
}
31 changes: 31 additions & 0 deletions app/Providers/ViewServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class ViewServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
View::composer(
'components.footer', 'App\Http\ViewComposers\SocialPostsComposer'
);
}
}
15 changes: 15 additions & 0 deletions app/SocialPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,19 @@
class SocialPost extends Model
{
protected $fillable = ['socialId'];

public function recent($limit=3) {
$posts = SocialPost::whereIn('socialId', ['facebook', 'twitter'])->orderBy('pubDate','desc')->limit($limit)->get();

$posts = $posts->map(function ($item) {
return [
'source' => $item->platform,
'data' => $item->text,
'url' => $item->socialUrl,
'time' => $item->pubDate,
];
});

return $posts;
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
App\Providers\ViewServiceProvider::class,

/*
* Package Service Providers...
Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/footer.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<h5>Social Media Feed</h5>
<div class="textwidget">
<ul class="fetched_tweets light">
@foreach ($twitterArray as $tweet)
@foreach ($twitterPosts as $tweet)
<li class="tweets_avatar">
<div class="tweet_wrap">
<div class="ltr">
Expand All @@ -38,7 +38,7 @@
<div class="widget widget_media_gallery">
<h5>Instagram Feed</h5>
<div class="gallery galleryid-25 gallery-columns-4 gallery-size-thumbnail">
@foreach ($instagramArray as $instagram)
@foreach ($instagramPosts as $instagram)
<dl class="gallery-item">
<dt class="gallery-icon {{isset($instagram['portrait']) && $instagram['portrait']?'portrait':'landscape'}}">
<img width="150" height="150" data-opt-src="{{$instagram['srcLarge']}}" src="{{$instagram['srcSmall']}}" class="attachment-thumbnail size-thumbnail hoverZoomLink" alt="" data-opt-loaded="true" data-opt-lazy-loaded="true" data-opt-otimized-width="75" data-opt-optimized-height="75" />
Expand Down