Laravel显示WHERE子句的所有帖子和laravel中的分页

问题描述:

so i want to display post in my index page but only the posts that are reviewed by the admin. so i added another column in my database table post which is called "review" which is integer. So this is what i have in hand.

in my controller i have this

public function index()
    {

        $this->layout->content = View::make('partials.index', 
            array(
            'posts' => Post::paginate(9)
            ));
    }

and in my index page i have this

@section('content')
    <div class="dashboard">
    @foreach (array_chunk($posts->getCollection()->all(),2) as $row)
        <div class="row">
            @foreach($row as $post)
                <article class="col-md-4 effect4" id="dash-box">
                    <p></p>

                    <c>{{$post->content}}</c><br>
                    <b>{{$post->title}}</b><br>
                    <d>posted..{{$post->created_at->diffForHumans()}}</d>
                    <hr>
                </article>
            @endforeach
        </div>
    @endforeach

    <div class="page">
    {{$posts->appends(Request::only('difficulty'))->links()}}
    </div>
    </div>
@stop

Help im newbie here i hope someone can help me out with this one hoping for a reply thanks

Just call:

Post::whereReview(1)->paginate(9);

Please read the documentation (and also the one for the query builder since these pages apply to Eloquent as well)

You can just add a where() call to the query:

$posts = Post::where('review', '=', 1)->paginate(9);

Or a shorter version (= is the default operator)

$posts = Post::where('review', 1)->paginate(9); 

Or even with a dynamic method name:

$posts = Post::whereReview(1)->paginate(9); 

Also you can use true instead of 1. Laravel will convert it:

$posts = Post::whereReview(true)->paginate(9);

There is also no need to do chunking that complicated:

@foreach (array_chunk($posts->getCollection()->all(),2) as $row)

You can just use the chunk method on any collection:

@foreach ($posts->chunk(2) as $row)