根据创建的时间显示订阅源和博客

时间:2016-07-13 20:52:15

标签: laravel laravel-4 laravel-5 laravel-5.1 laravel-5.2

我有一个主页,其中显示所有供稿以及评论和博客。现在它首先显示所有Feed,然后是博客。但我希望它应该根据创建的时间显示,饲料和博客的混合器不像第一个饲料将来,然后博客。任何人都可以告诉我如何实现这一点。这是我的 index.blade.php

d

@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>

这是 action.partials.feed

@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

action.partials.blogfeed

@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

这是我的 feedcontroller

@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

任何人都可以告诉我如何根据公布的时间显示供稿和博客。

2 个答案:

答案 0 :(得分:1)

在您的控制器index方法中,尝试以下方法:

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'));
}

您遇到的最大问题是您的Feed对象可能与您的Blog对象不同。意味着每个都有唯一的列名,而另一个没有。这将使foreach有点混乱......

删除@include ('action.partials.blogfeed')。这与我们不再相关。

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>            

            // 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

答案 1 :(得分:0)

由于Illuminate\Database\Eloquent扩展了Illuminate\Support\Collection,我们可以轻松地将它们合并在一起。由于$feeds$blogs都是Eloquent Collection,因此我们可以轻松地将它们合并到一个集合中:

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

现在你将两者结合起来。由于您在迁移中使用$table->timestamps()来获取列,因此我们可以轻松地对created_at时间戳进行比较,以获得所需的排序:

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

但是,您可能希望首先按newest对它们进行排序。值得庆幸的是,collections具有sortByDesc功能,可以完全按照我们的要求进行操作。

虽然在宏观方案中你可能真的想要Polymorphic relationship