【发布时间】:2014-11-26 19:28:28
【问题描述】:
我有一个 Laravel PHP Photo Gallery 应用程序,它允许用户创建相册,然后从下拉列表中将照片插入到他们选择的相册中。这需要用户首先创建相册,因为下拉列表是动态的,基于实际存在的相册。下拉列表当前运行正常,但不按字母顺序显示专辑(数组元素)。
我尝试过使用 'sort()' 方法,但问题是数组元素 id 在这样做时会被切换/重新排序。我不希望对数组 ID 进行重新排序,因为这会将照片放入错误的相册中。
所以我想知道是否有一种方法可以在下拉列表中按字母顺序显示专辑,同时不对它们的数组元素 ID 重新排序。
控制器:
public function create()
{
$stoneArray = $this->stone->all()->toArray();
if (empty($stoneArray)) {
$dropdown[0] = 'There are no stones';
}
foreach ($stoneArray as $stone) {
$dropdown[$stone['stone_id']] = $stone['stone_name'];
// sort($dropdown); /* This re-sorted the array element ids */
}
$data = array('type' => 'stone_photo', 'dropdown' => $dropdown);
$this->layout->content = \View::make('forms.stones.new-photo', $data);
}
查看:
<div class="form-group">
{{ Form::label('stone_id', 'Stone: ') }}
{{ Form::select('stone_id', $dropdown, array('class' => 'form-control')) }}
</div>
非常感谢任何帮助!谢谢!
【问题讨论】:
-
stone是数据库模型吗? -
@TimLewis 是的,“石头”是模特。