Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nhayder's avatar
Level 13

5 records in query, 1st should be Featured article, reset should be looped through in blade

in eloquent i'm running this query,

$news = Post::where('status','=',1)
    ->orderBy('id', 'Desc')
    ->limit(5)
    ->get();

the query is getting last 5 records from news table? the issue i'm facing is in blade? the first record of the query should be featured news where in blade i'm using bootstrap to get a big image covering half of the row (col-md-6) and the reset of the posts should be presented in the other half of the row element separated in 4 cells (col-md-3 for each).

how can i solve this problem by separating the query and take first post to show in featured area and loop the rest of the 4 remaining posts in smaller cells.

im using this currently

@foreach($news as $item)
    @if ($loop->first)
        // here is the featured post
    @else
        // these are the remaining posts
    @endif

@endforeach

the loop above is separating the first post as featured but it still looping the 5 records in the query in the smaller cells.

so basically i have the featured post repeated with the rest of the posts.

Example : 5 queries first is featured the reset are not feature, the solution above is returning

@foreach($news as $item)
    @if ($loop->first)
        // featured post is returned correctly 
    @else
        // here? blade is looping the entire query including featured post, (featured post is repeated in this area also)
    @endif

@endforeach

is there is a solutions so i can loop the remaining last 4 records only

0 likes
4 replies
Sergiu17's avatar

Your post is quite long and I'm too lazy to read it.

how can i solve this problem by separating the query and take first post to show in featured area and loop the rest of the 4 remaining posts in smaller cells.

$news = Post::where('status','=',1)
    ->orderBy('id', 'Desc')
    ->limit(5)
    ->get();

$featuredPost = $news->shift();

Now $news contains 4 items and $featuredPost just featured post.

nhayder's avatar
Level 13

@Sergiu17 ok if we want to go this way i can get $new to get last entered post Thats easy, But my question will be how to make a $featuredPost query where it ignores the last record (featured) and get the rest of the 4 records.

Example : Query should ignore last entered records and take 2nd+3rd+4th and 5th records (first is the featured post).

is that doable ?

Cronix's avatar
Cronix
Best Answer
Level 67

@nhayder Why do you need to do that when @sergiu17 solution does basically that?

$news = Post::where('status','=',1)
    ->orderBy('id', 'Desc')
    ->limit(5)
    ->get();

gets 5 most recent posts

$featuredPost = $news->shift();

takes/removes the first post from $news (the featured post) and stores it as $featuredPost. So $featuredPost is now the single featured post, and $news now only contains the last 4 since the first result was removed from it and stored as $featuredPost.

2 likes

Please or to participate in this conversation.