N
N
nevesomostjke2022-03-05 02:59:11
Laravel
nevesomostjke, 2022-03-05 02:59:11

How to display specific array value at the top?

I have this foreach

@foreach ($parts as $part)
{{ $part->name }}
@if($part->stock)
В наличии
@endif
@endforeach


It turns out that it is displayed like this:
- product
- product
- product in stock
- product

And I want the products in stock to be the first. Ie:
- the goods are in stock.
- product.
- product.
- product.

Please tell me how to solve.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2022-03-05
@nevesomostjke

https://laravel.com/docs/9.x/collections#method-sortby

$collection = collect([
    ['name' => 'Desk', 'price' => 200],
    ['name' => 'Chair', 'price' => 100],
    ['name' => 'Bookcase', 'price' => 150],
]);
 
$sorted = $collection->sortBy('price');
 
$sorted->values()->all();

/*
    [
        ['name' => 'Chair', 'price' => 100],
        ['name' => 'Bookcase', 'price' => 150],
        ['name' => 'Desk', 'price' => 200],
    ]
*/

Or just sorting

J
jazzus, 2022-03-05
@jazzus

orderBy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question