0

I'm having a home page where all the feeds along with comments and blogs are being displayed. Right now it is showing first all the feeds and then the blog. But I want it should be displayed according to the created time, mixer of feeds and blog not like first feed will come and then the blog. Can anyone tell me how to acheive that. This is my index.blade.php

@extends('layouts/default')

{{-- Page title --}}
@section('title')
Home
@parent
@stop

{{-- page level styles --}}
 @section('header_styles')
<!--page level css starts-->
<link rel="stylesheet" type="text/css" href="{{ asset('assets/css/frontend/action.css') }}">
<link rel="stylesheet" type="text/css" href="{{ asset('assets/css/frontend/tabbular.css') }}">
<link rel="stylesheet" type="text/css" href="{{ asset('assets/css/frontend/jquery.circliful.css') }}">
<link rel="stylesheet" type="text/css" href="{{ asset('assets/vendors/owl.carousel/css/owl.carousel.css') }}">
<link rel="stylesheet" type="text/css" href="{{ asset('assets/vendors/owl.carousel/css/owl.theme.css') }}">
<!--end of page level css-->
@stop

{{-- content --}}
@section('content')

<div class="row">
    <div class="column col-md-1 col-xs-1 col-sm-1"></div>
    <div class="column col-md-3 col-xs-3 col-sm-3"><!--gm-editable-region--> </div>

    <div class="column col-md-4 col-xs-4 col-sm-4">

       @include ('action.partials.form')

       @include ('action.partials.error')

       @include ('action.partials.feed')

       @include ('action.partials.blogfeed')

    </div>

    <div class="column col-md-3 col-xs-3 col-sm-3"><!--gm-editable-region--></div>
    <div class="column col-md-1 col-xs-1 col-sm-1"></div>

@stop
{{-- footer scripts --}}
@section('footer_scripts')
<!-- page level js starts-->
<script type="text/javascript" src="{{ asset('assets/js/frontend/jquery.circliful.js') }}"></script>
<script type="text/javascript" src="{{ asset('assets/vendors/owl.carousel/js/owl.carousel.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('assets/js/frontend/carousel.js') }}"></script>
<script type="text/javascript" src="{{ asset('assets/js/frontend/index.js') }}"></script>
<!--page level js ends-->

@stop

This is action.partials.feed

@foreach($feeds as $feed)
<article class="media">
    <div class="well">
        <div class="pull-left"> 
            <img class="profile" src="{{ URL::to('/uploads/users/'.$feed->user->pic)  }}" class="img-responsive" alt="Image" style="width:48px;height:48px;padding-right : 10px;padding-bottom: 5px;">
         </div>
        <strong>{{ $feed->user->first_name }} 
                {{ $feed->user->last_name }}
                <small> posted </small>
         </strong>
                {{ $feed->created_at->diffForHumans() }}<br><hr>
                {{ $feed->feed_content }}

            <hr>
            {!! Form::open(['url' => 'home/{storecomment}']) !!}
                <div><input type="hidden" name="feed_id" value="{{ $feed->feed_id }}" /></div>
                <div class="form-group">
                    {!! Form::text('comment', null, ['class'=>'form-control', 'rows'=>3, 'placeholder'=>"Comment"]) !!}
                </div>

                <div class="form-group feed_post_submit">
                    <a href="/home">{!! Form::submit('Comment', ['class' => 'btn btn-default btn-xs']) !!}</a>
                </div>
            {!! Form::close() !!}

            @foreach($feed->comments as $comment)
                <div class="pull-left"> 
                    <img class="profile" src="{{ URL::to('/uploads/users/'. $comment->user->pic)  }}" class="img-responsive" alt="Image" style="width:48px;height:48px;padding-right : 10px;padding-bottom: 5px;">
                </div>
                {{ $comment->user->first_name }} 
                {{ $comment->created_at->diffForHumans() }}
                {{ $comment->comment }}<hr>
            @endforeach
    </div>
</article>
@endforeach

action.partials.blogfeed

@foreach($blogs as $blog)
<article class="media">
    <div class="well">
        <div class="pull-left"> 
            <img class="media-object" src="{{ URL::to('/uploads/users/'.$blog->user->pic)  }}" class="img-responsive" alt="Image" style="width:48px;height:48px;padding-right : 10px;padding-bottom: 5px;">
        </div>
        <strong>{{ $blog->user->first_name }} 
                {{ $blog->user->last_name }}
                <small> posted blog</small>
        </strong>
                {{ $blog->created_at->diffForHumans() }}<br><hr>
                <h4><a href="{{ URL::to('blogitem/'.$blog->slug) }}">{{ $blog->title }}</a></h4>
                <div class="featured-post-wide thumbnail">
                     @if($blog->image)
                        <img src="{{ URL::to('/uploads/blog/'.$blog->image)  }}" class="img-responsive" alt="Image">
                     @endif
                 </div>
        </div>
</article>
@endforeach

This is my feedcontroller

<?php

namespace App\Http\Controllers;

use Request;
use Auth;
use Sentinel;
use App\Feed;
use App\Http\Requests;
use App\Blog;
use App\Http\Controllers\Controller;
use App\Comment;

class FeedsController extends Controller
{
    public function index() {

  // $comments = Comment::latest()->get();
  $feeds = Feed::with('comments', 'user')->where('user_id', Sentinel::getUser()->id)->latest()->get(); 
  $blogs = Blog::latest()->simplePaginate(5);
  $blogs->setPath('blog');
  return view('action.index', compact('feeds', 'blogs'));
}

public function store(Requests\CreateFeedRequest $request)
{
  $request->merge( [ 'user_id' => Sentinel::getuser()->id] );
  Feed::create($request->all());
  return redirect('home');
}

 public function storecomment(Requests\CommentRequest $request, Feed $feed)
 {

  $comment = new Comment;
  $comment->user_id =Sentinel::getuser()->id;
  $comment->feed_id = $request->feed_id;
  $comment->comment = $request->comment;
  $comment->save();
  return redirect('/home');
}  
}

Can any one tell me how to display feeds and blogs according to the published time.

Vikram Shaw
  • 11
  • 1
  • 7

2 Answers2

1

In your controller index method, try something like this:

public function index() {
    // $feeds = Feed::with('comments', 'user')->where('user_id', Sentinel::getUser()->id)->latest()->get(); 
    // $blogs = Blog::latest()->simplePaginate(5);
    // $blogs->setPath('blog');

    $feeds = Feed::with('comments', 'user')->where('user_id', Sentinel::getUser()->id)->latest()->get();
    $blogs = Blog::latest()->paginate(5);
    $feeds = $feeds->merge($blogs)->sortByDesc('created_at');

    return view('action.index', compact('feeds'));
}

The biggest problem you're going to have is that your Feed object is likely different than your Blog object. Meaning each will have unique column names that the other doesn't have. This is going to make doing the foreach a bit of a mess...

Remove @include ('action.partials.blogfeed'). This is not longer relevant for us.

In action.partials.feed...we'll output it all (hopefully without too many "hacks" and conditionals):

@foreach($feeds as $feed)
    <article class="media">
        <div class="well">
            <div class="pull-left"> 
                <img class="profile" src="{{ URL::to('/uploads/users/'.$feed->user->pic)  }}" class="img-responsive" alt="Image" style="width:48px;height:48px;padding-right : 10px;padding-bottom: 5px;">
            </div>

            <strong>
                {{ $feed->user->first_name }} 
                {{ $feed->user->last_name }}
                <small> posted </small>
            </strong>            

            // We'll use @if(isset($feed->title)) to check if it's a blog post, aka ugly hack.
            @if(isset($feed->title))
                {{ $blog->created_at->diffForHumans() }}
                <br><hr>
                <h4><a href="{{ URL::to('blogitem/'.$blog->slug) }}">{{ $blog->title }}</a></h4>
                <div class="featured-post-wide thumbnail">
                     @if($blog->image)
                        <img src="{{ URL::to('/uploads/blog/'.$blog->image)  }}" class="img-responsive" alt="Image">
                     @endif
                 </div>
            @else
                {{ $feed->created_at->diffForHumans() }}<br><hr>
                {{ $feed->feed_content }}

                <hr>

                {!! Form::open(['url' => 'home/{storecomment}']) !!}
                    <div><input type="hidden" name="feed_id" value="{{ $feed->feed_id }}" /></div>
                    <div class="form-group">
                        {!! Form::text('comment', null, ['class'=>'form-control', 'rows'=>3, 'placeholder'=>"Comment"]) !!}
                    </div>

                    <div class="form-group feed_post_submit">
                        <a href="/home">{!! Form::submit('Comment', ['class' => 'btn btn-default btn-xs']) !!}</a>
                    </div>
                {!! Form::close() !!}

                @foreach($feed->comments as $comment)
                    <div class="pull-left"> 
                        <img class="profile" src="{{ URL::to('/uploads/users/'. $comment->user->pic)  }}" class="img-responsive" alt="Image" style="width:48px;height:48px;padding-right : 10px;padding-bottom: 5px;">
                    </div>
                    {{ $comment->user->first_name }} 
                    {{ $comment->created_at->diffForHumans() }}
                    {{ $comment->comment }}<hr>
                @endforeach
            @endif     
        </div>
    </article>
@endforeach
Mike Barwick
  • 6,288
  • 6
  • 51
  • 76
0

Because Illuminate\Database\Eloquent extends Illuminate\Support\Collection, we can easily merge them together. Because both $feeds and $blogs are an Eloquent Collection, we can easily merge them into one collection:

 $feeds_and_blogs = collect($feeds->toArray(), $blogs->toArray());

Now you will have a combination of both. Because you've used $table->timestamps() in your migration to get your columns, we can easily perform a comparison against the created_at timestamp to get the sorting that you want:

$sorted_feeds_and_blogs = $feeds_and_blogs->sortBy(function($item){
    return $item->created_at;
});

However, you likely want them to be sorted by newest first. Thankfully, collections have a sortByDesc function which will do exactly what we want.

Although in the grand scheme of things you probably really want a Polymorphic relationship.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • @MikeBarwick Let me see what I can come up with for you. In the mean time [here's a good read](http://stackoverflow.com/questions/37931477/multiple-tables-with-one-to-many-relationshiplaravel/37934163#37934163) from another answer regarding the polymorphic implementation. – Ohgodwhy Jul 13 '16 at 22:15
  • I'm not the questioneer, simply a curious mind. I'll have a read. :) – Mike Barwick Jul 13 '16 at 22:17