如何通过使用两行在单个刀片中显示多个帖子(示例起始帖子,expired_date帖子)

时间:2019-06-13 06:16:55

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

我有一个类别页面,在这里我要在单页中显示两种类型的框,一种是starting_postold_postexpired。如何将一个页面分为两部分?

class FrontendController extends Controller
{
    public function welcome()
    {

        // @TODO Refactor This Line
        return view('welcome')

            ->with('title', Setting::first()->site_name)
            ->with('levels', Category::take(7)->get())

            ->with('levels', Category::take(7)->get())
            ->with('first_post', Post::orderBy('created_at','asc')->first())
            ->with('second_post', Post::orderBy('created_at', 'asc')->skip(1)->take(1)->get()->first())
            ->with('third_post', Post::orderBy('created_at','asc')->skip(2)->take(2)->get()->first())
            ->with('forth_post', Post::orderBy('created_at','asc')->skip(3)->take(3)->get()->first())
            ->with('HOME', Category::find(1))
            ->with('ABOUT US', Category::find(2))
            ->with('RESEARCH', Category::find(3))
            ->with('NEWS AND PUBLICATION', Category::find(4))
            ->with('EVENTS', Category::find(5))
            ->with('PEOPLE', Category::find(6))
            ->with('CONTACT US',Category::find(7));
    }

    public function singlePost($slug)
    {
        $post=Post::whereSlug($slug)->first();

        return view('single')->with('post', $post)
                       ->with('content', $post)
                       ->with('levels', Category::take(7)->get());
    }

    public function category($slug)
    {
       $category=Category::whereSlug($slug)->firstOrFail();

       return view($category->getTemplateFile())->with('category', $category)
                     ->with('title',$category->name)
                     ->with('levels', Category::take(7)->get());

    }

    public function events($slug)
    {
       $post = Post::where('slug', $slug)->first();

       $coming_events = Post::where('id', '>', $post->id)->desc('id');
       $past_events = Post::where('id', '<', $post->id)->asc('id');

       return view('events_blade')
                ->with('post', $post)
                ->with('categories', Category::take(7)->get())
                ->with('next', Post::find($coming_events))
                ->with('prev', Post::find($past_events));
    }
}

//查看|刀片

<div class="entry-meta">
<span>{{ \Carbon\Carbon::parse($post->date_time)->format('F j, Y') }}</span> 
<div>
<h class="entry-title">
{{ $post->title }}</h3> 
<div class="entry-content">
   <a target="_blank" href="{{ asset("$post->
      file") }}">
      <p style="font-size: 18px;color: #468b10;"> {{ $post->content }}</p>
   </a>
</div>

3 个答案:

答案 0 :(得分:0)

这样更改事件功能。查询构建器出现问题,并且您为视图设置的值不正确。

public function events($slug)
{
   $post = Post::where('slug', $slug)->first();

   $coming_events = Post::where('id', '>', $post->id)->orderBy('id', 'desc')->get();
   $past_events = Post::where('id', '<', $post->id)->orderBy('id', 'asc')->get();

   return view('events_blade')
            ->with('post', $post)
            ->with('categories', Category::take(7)->get())
            ->with('coming_events', $coming_events)
            ->with('past_events', $past_events);
}

分别在这两个变量的events_blade.blade.php循环中。

// show upcoming events.

@foreach($coming_events as $ce)
  <div class="entry-meta">
  ...
  <h3> {{ $ce->title }}</h3> 
  ...
  </div>
@endforeach




// show past events.

@foreach($past_events as $pe)
  <div class="entry-meta">
  ...
  <h3> {{ $pe->title }}</h3> 
  ...
  </div>
@endforeach

有关详细信息,请查阅laravel文档以设置刀片的值。

答案 1 :(得分:0)

尝试

public function events($slug)
    {
       $post = Post::where('slug', $slug)->first();

       $data['coming_events'] = Post::where('id', '>', $post->id)->orderBy('id', 'desc')->get();
       $data['past_events'] = Post::where('id', '<', $post->id)->orderBy('id', 'asc')->get();

       return view('events_blade', $data)

    }



<div class="row">

   <div class="col-sm-4">
     <i>current events</i>
     @foreach($coming_events as $new)

           {{ $new->title}}

     @endforeach

    </div>
    <div class="col-sm-4">

    <i>Past events</i>
     @foreach($past_events as $old)

           {{ $old->title}}

     @endforeach

    </div> 
</div>

答案 2 :(得分:0)

那你就必须这样:

public function events($slug)
{
       $post= Post::where('slug', $slug)->first();
       $data['post'] = Post::where('slug', $slug)->first();

       $data['coming_events'] = Post::where('id', '>', $post->id)->orderBy('id', 'desc')->get();
       $data['past_events'] = Post::where('id', '<', $post->id)->orderBy('id', 'asc')->get();

       data['categories']=Category::take(7)->get();

       return view('events_blade', $data)
}