如何使用两行在单个 blade 中显示多个 post(示例从 post、expired_date post 开始)

How to display multiple post in single blade by using two row(example starting post, expired_date post)

我有一个类别页面,在此我想在单个页面中显示两种类型的框,一种是 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));
    }
}

// 查看 | blade

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

像这样更改您的活动功能。您的查询生成器有问题,您的视图设置值不正确。

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 文档以设置 blade 的值以获取更多详细信息。

试试这个

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>

那么你必须这样做:

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