Filters some Collection object

Posted 8 months ago by guillermo_rojas

Hi Everyone: I'm stuck in the middle of something.7

Lets say that I have some of data like this

user::all()

If I wanna show them in my view, I'd do it like

@foreach ($users as user)
    {!! $user->name !!}
    {!! $user->active !!}
    {!! $user->created_at !!}
@endforeach

Correct?

But what if I want to show, for example, the latest created today, and then the rest.

// (today is 22nd april)

Our latest users

  • Jotaro (created_at April 22nd 2018 11:00:00, true)
  • Jane (created_at April 22nd 2018 13:00:00, true)

The rest

  • John (created_at April 22nd 2018 12:00:00, false)
  • Jolyne (created_at April 22nd 2018 12:30:00, true)
  • Peter (created_at April 22nd 2018 14:00:00, false)
  • Josuke (created_at April 21st 2018 13:00:00, false)
  • Johnny (created_at April 21st 2018 13:00:00, false)
  • Johnathan (created_at April 21st 2018 13:00:00, false)

As you can see, there are five users who were created in april 22nd, but I'm interested the latest two who are active, and then the rest regardless their creation time.

If Jolyne becomes 'active', Jolyne should be in our latest users, and Jane in 'The rest'

How could I achieve this? Thank you

I'm hoping to do it like

//Latest  2 active users
@foreach ($users as user)
    {!! $user->name !!}
    {!! $user->active !!}
    {!! $user->created_at !!}
@endforeach 

//The rest
@foreach ($others_users as user)
    {!! $user->name !!}
    {!! $user->active !!}
    {!! $user->created_at !!}
@endforeach 

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.