如何在 PHP Laravel 中使下拉列表升序排列

How to make dropdown list ascending order in PHP Laravel

此代码来自 product_listing.blade.php,这里是下拉过滤器。我需要按升序排列的所有项目名称。现在这个列表是为了我如何在数据库中看到它 table..

                                    <div class="box-title text-center">
                                        {{ translate('Filter by CarType')}}
                                    </div>
                                    <div class="box-content">
                                        <div class="filter-checkbox">
                                            <select class="form-control" data-placeholder="{{ translate('All CarType')}}" name="cartype" onchange="filter()">
                                                <option value="">Select CarType</option>
                                                @foreach (\App\CarType::all() as $cartype)
                                                    <option value="{{ $cartype->id }}" @isset($cartype_id) @if ($cartype_id == $cartype->id) selected @endif @endisset>{{ $cartype->name }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>

您可以使用集合的 sortBy() 方法:

                                    <div class="box-title text-center">
                                        {{ translate('Filter by CarType')}}
                                    </div>
                                    <div class="box-content">
                                        <div class="filter-checkbox">
                                            <select class="form-control" data-placeholder="{{ translate('All CarType')}}" name="cartype" onchange="filter()">
                                                <option value="">Select CarType</option>
                                                @foreach (\App\CarType::all()->sortBy('name') as $cartype)
                                                    <option value="{{ $cartype->id }}" @isset($cartype_id) @if ($cartype_id == $cartype->id) selected @endif @endisset>{{ $cartype->name }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>