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

SarahS's avatar
Level 12

Ordering results

I have some code in my controller that pulls back all Types for a particular Client and then groups them so I can show them with sub-headings in my View:

$profile = Type::orderBy('type')->where('client_id', $client->id)->get();
        $profile = $profile->groupBy(function ($type) {
            return $type->type;
        })->all();

How can I order the Types based on a particular order that is not alphabetical?

For example, say my Types are Type A, Type B, Type C but I want to show them:

Type B
Type C
Type A
0 likes
29 replies
ftiersch's avatar

Is that order dynamic? Or do you want to do that manually?

You could have a second array with your types in the wanted order and then in your blade cycle through that array and display the profiles with the given type. So if you want to reorder you only need to change that array.

SarahS's avatar
Level 12

The order is not dynamic, it will always be a set order.

ftiersch's avatar
$types = ['Type B', 'Type C', 'Type A'];

foreach ($types as $type) {
    foreach ($profiles->where('type', $type) as $profile) {

    }
}

I would probably do it like this. (Only that the "foreach" part would be in the blade :))

Sti3bas's avatar

@sarahs74 try this:

$sortValues = ['Type B' => 1, 'Type C' => 2, 'Type A' => 3];

$sorted = $collection->sortBy(function ($item) use ($sortValues) {
   return $sortValues[$item->type];
});
ftiersch's avatar

Well as @sti3bas said you could use sortBy...

$types = ['Type B', 'Type C', 'Type A'];

$profile->sortBy(function ($profile) use ($types) {
    return array_search($profile->type, $types);
});

Maybe something like that.

1 like
SarahS's avatar
Level 12

I've added that to my Controller but I get:

Call to a member function sortBy() on array

am I putting it in the right place?

My original Controller was:

        $profile = Type::orderBy('type')->where('client_id', $client->id)->get();
        $profile = $profile->groupBy(function ($type) {
            return $type->type;
        })->all();
       
        
        return view('clients.show', compact('client', 'profile'));

and my View:

        @foreach($profile as $type => $types)
                            {{ $type }}
                            <ul>
                                @foreach ($types as $type)
                                    <li><a href="/clients/{{ $client->id }}/types/{{ $type->id }}">{{ $type->name }}</a></li>
                                @endforeach
                            </ul>
                        @endforeach 

Sti3bas's avatar

@sarahs74 add sortBy before all. sortBy is a collection method and all returns an array.

munazzil's avatar

You have to do something changes because when you use orderBy you have to use asc or desc and you want to use get end of the query and return with your $profile not with $type as like below,

    $profile = Type::orderBy('type')->where('client_id', $client->id)->get();
    $profile = $profile->groupBy(function ($type) {
        return $profile ->type;
    })->asc()->get();
SarahS's avatar
Level 12
        $types = ['Individual' => 1, 'Sole Trade/Partnership' => 2, 'Company' => 3];

        $profile = Type::orderBy('type')->where('client_id', $client->id)->get();

        $profile->sortBy(function ($profile) use ($types) {
            return array_search($profile->type, $types);
        });

        $profile = $profile->groupBy(function ($type) {
            return $type->type;
        })->all();
        
        return view('clients.show', compact('client', 'profile'));
Sti3bas's avatar

@sarahs74 try this:

$types = ['Individual', 'Sole Trade/Partnership', 'Company'];

$profile = Type::orderBy('type')
    ->where('client_id', $client->id)
    ->get()
    ->groupBy(function ($type) {
        return $type->type;
    })->sortBy(function ($profile) use ($types) {
        return array_search($profile->type, $types);
    })->all();

return view('clients.show', compact('client', 'profile'));
SarahS's avatar
Level 12

It has come back with this but I'm not sure what because it was OK before:

Property [type] does not exist on this collection instance.
SarahS's avatar
Level 12

it's this line it doesn't like type in:

return array_search($profile->type, $types);
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

@sarahs74 does it work this way?:

$types = ['Individual', 'Sole Trade/Partnership', 'Company'];

$profile = Type::orderBy('type')
    ->where('client_id', $client->id)
    ->get()
    ->sortBy(function ($profile) use ($types) {
        return array_search($profile->type, $types);
    })
    ->groupBy(function ($type) {
        return $type->type;
    })->all();

return view('clients.show', compact('client', 'profile'));
SarahS's avatar
Level 12

I think it's because it is returning as a Collection and there is no type in the Collection, it's down one level. I'm not sure how to order the Collection.

Sti3bas's avatar

@sarahs74 what about this?:

$types = ['Individual', 'Sole Trade/Partnership', 'Company'];

$profile = Type::orderBy('type')
    ->where('client_id', $client->id)
    ->get()
    ->groupBy(function ($type) {
        return $type->type;
    })
    ->sortBy(function ($profile, $key) use ($types) {
        return array_search($key, $types);
    })->all();

return view('clients.show', compact('client', 'profile'));
Sti3bas's avatar

@sarahs74 can you dd the $profile and post the result? So that we could see a structure.

SarahS's avatar
Level 12

Actually, with the SortBy I get:

array:3 [▼
  "Sole Trader/Partnership" => Collection {#530 ▶}
  "Individual" => Collection {#509 ▶}
  "Company" => Collection {#494 ▶}
]

without it I get:

array:3 [▼
  "Company" => Collection {#494 ▶}
  "Individual" => Collection {#509 ▶}
  "Sole Trader/Partnership" => Collection {#530 ▶}
]

So it's sorting alphabetically I think.

ftiersch's avatar

Yeah, because you order the types alphabetically in your query.

SarahS's avatar
Level 12

@ftiersch But I thought the SortBy in @sti3bas's solution was to sort by the array of $types at the top of that code extract.

ftiersch's avatar

Yes the sortby sorts the "outer" collection. That's why it isn't alphabetical anymore when you use it.

I'm not sure why it doesn't have the correct order with his code - looks correct to me.

Sti3bas's avatar

@sarahs74 you have a typo in "Sole Trade/Partnership". I guess it should be Sole Trader/Partnership?

$types = ['Individual', 'Sole Trader/Partnership', 'Company'];

SarahS's avatar
Level 12

Ugh! That's solved it! Thanks for your solutions.

1 like

Please or to participate in this conversation.