根据创建的时间显示提要和博客 [英] Displaying feeds and blogs according to the created time

查看:49
本文介绍了根据创建的时间显示提要和博客的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主页,其中显示了所有提要以及评论和博客.现在,它首先显示所有提要,然后显示博客.但是我希望它应该根据创建的时间,提要和博客的混合显示,而不是先出现提要然后再出现博客.谁能告诉我如何做到这一点.这是我的 index.blade.php

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

这是 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

这是我的 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.

推荐答案

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

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

最大的问题是Feed对象可能与Blog对象不同.意思是每个将具有另一个没有的唯一列名.这会使foreach有点混乱...

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

删除@include ('action.partials.blogfeed').这对我们不再重要.

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

action.partials.feed 中...我们将全部输出(希望没有太多的"hack"和条件):

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

这篇关于根据创建的时间显示提要和博客的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆