选择的PHP Laravel选项无法呈现正确的值 [英] PHP Laravel options of select not rendering correct values
本文介绍了选择的PHP Laravel选项无法呈现正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用laravel-nova语法在选择内显示值数组作为选项.我设法将选择内的选项渲染出来,但是这些选项的值就像
I want to display an array of values as options inside a select using the laravel-nova syntax. I managed to get the options rendered inside the select but the values of these options are like
<option value="2"></option>
<option value="1"></option>
<option value="0"></option>
我想要的是文本作为值.
what I want is the text as an value.
这是我到目前为止所得到的:
this is what I got so far:
Select::make('Slug')->options(
$this->selectOptions()
)
public function selectOptions()
{
$urls = DB::table('subpages');
$slugs = $urls->pluck('slug');
return $slugs;
}
我在做什么错了?
推荐答案
确保将get()
子页面分别作为Illuminate\Support\Collection
和mapWithKeys()
来重新格式化结果.使用toArray()
提供Nova假定的格式:
Make sure to get()
the subpages as Illuminate\Support\Collection
and mapWithKeys()
to reformat the results. Use toArray()
to provide the format Nova assumes:
private function selectOptions(): array
{
$subpages = DB::table('subpages')->get();
return $subpages->mapWithKeys(function ($subpage) {
return [$subpage->slug => $subpage->slug];
})->toArray();
}
这是返回结果的样子:
[
'my-article-1' => 'my-article-1',
'my-article-2' => 'my-article-2',
'my-article-3' => 'my-article-3',
]
这篇关于选择的PHP Laravel选项无法呈现正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文