douyi5157 2018-11-17 02:15
浏览 130

Laravel采用跳过查询而不采用有效数据

I am developing a Laravel blogging app.

I am trying to fetch articles in the database using a query builder but it doesn't seem to work for me.

What I want is to skip the top 3 latest articles and display only the 2 next articles (4th and 5th)

Here is my function:

public function getIndex() {
    $posts = Post::orderBy('created_at', 'desc')->skip(3)->take(2)->get();
    return view('pages.home')->with(['posts' => $posts]);
}

My blade view is structured like this:

<div class="col-lg-4 top-post-right">
    @foreach ($posts as $post)
        <div class="single-top-post">
            <div class="feature-image-thumb relative">
                <div class="overlay overlay-bg"></div>
                <img class="img-fluid" src="/storage/{{$post->image}}" alt="">
            </div>
            <div class="top-post-details">
                <ul class="tags">
                    <li><a href="#">{{ $post->category->name }}</a></li>
                </ul>
                <a href="{{ url('article/'.$post->slug) }}">
                    <h4>{{ $post->title }}</h4>
                </a>
                <ul class="meta">
                    <li><a href="#"><span class="lnr lnr-user"></span>Mark wiens</a></li>
                    <li><a href="#"><span class="lnr lnr-calendar-full"></span>{{date('j M, Y',strtotime($post->created_at))}}</a></li>
                    <li><a href="#"><span class="lnr lnr-bubble"></span>{{ $post->comments()->count() }} Comments</a></li>
                </ul>
            </div>
        </div>                    
    @endforeach
</div>

This is printing all the articles, does anyone know where I messed up?

</div>
  • 写回答

1条回答 默认 最新

  • dourun2990 2018-11-17 10:54
    关注

    this actually works on me as ive tried:

    $posts = Post::orderBy('created_at', 'desc')->skip(3)->take(2)->get();

    1.) your looking at the wrong blade file? or

    2.) must be a cache problem, restart server.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号