laravel在其中保留数组使用的顺序
因此,我将一些项目取出我的数据库以用于下拉列表。我希望下拉列表以输入的顺序显示这些项目,例如,我想订购的项目为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
orderbyraw
,例如:you can use
orderByRaw
such as:我解决了。我用它代替了构造函数:
I worked it out. I replaced my constructor with this: