laravel在其中保留数组使用的顺序

发布于 2025-02-08 18:00:38 字数 1057 浏览 3 评论 0原文

因此,我将一些项目取出我的数据库以用于下拉列表。我希望下拉列表以输入的顺序显示这些项目,例如,我想订购的项目为2,1,3。当我呈现结果时,结果是用ID对,而不是按照我想要的顺序排序:

<?php

namespace App\View\Components;

use App\Models\JobState;
use Illuminate\View\Component;

class StatusDropdown extends Component
{
    public $states;

    public function __construct()
    {
        $this->states = JobState::whereIn( 'id', [4, 5, 10, 3, 11] )->get();
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.status-dropdown');
    }
}

这是我的观点:

<select name="status" class="pl-10 block w-full shadow-sm sm:text-lg bg-gray-50 border-gray-300 focus:ring-primary-500 focus:border-primary-500 rounded-md">
    @foreach( $states as $state )
        <option value="{{ $state->id }}">{{ $state->value }}</option>
    @endforeach
</select>

我该如何执行此操作而不一次手动将它们添加或添加新列?

谢谢

So I'm getting some items out my database to use in a dropdown. I wish to have the dropdown show the items in the order these are entered so for example i want to have items ordered as 2,1,3. When i render the result the results are sorted by id and not in the order i desire:

<?php

namespace App\View\Components;

use App\Models\JobState;
use Illuminate\View\Component;

class StatusDropdown extends Component
{
    public $states;

    public function __construct()
    {
        $this->states = JobState::whereIn( 'id', [4, 5, 10, 3, 11] )->get();
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.status-dropdown');
    }
}

and here is my view:

<select name="status" class="pl-10 block w-full shadow-sm sm:text-lg bg-gray-50 border-gray-300 focus:ring-primary-500 focus:border-primary-500 rounded-md">
    @foreach( $states as $state )
        <option value="{{ $state->id }}">{{ $state->value }}</option>
    @endforeach
</select>

How can i do this without manually getting them one at a time or adding a new column?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁〃客ぐ 2025-02-15 18:00:39

您可以使用orderbyraw,例如:

JobState::whereIn( 'id', [4, 5, 10, 3, 11])->orderByRaw('FIELD(id, 4, 5, 10, 3, 11)')->get();

you can use orderByRaw such as:

JobState::whereIn( 'id', [4, 5, 10, 3, 11])->orderByRaw('FIELD(id, 4, 5, 10, 3, 11)')->get();
甜中书 2025-02-15 18:00:39

我解决了。我用它代替了构造函数:

    public function __construct()
    {
        $order = [4, 5, 10, 3, 11];
        $states = JobState::whereIn( 'id', $order )->get();
        $this->states = $states->sortBy(function( $model ) use ( $order ) {
            return array_search( $model->getKey(), $order );
        });
    }

I worked it out. I replaced my constructor with this:

    public function __construct()
    {
        $order = [4, 5, 10, 3, 11];
        $states = JobState::whereIn( 'id', $order )->get();
        $this->states = $states->sortBy(function( $model ) use ( $order ) {
            return array_search( $model->getKey(), $order );
        });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文