将id(整数)转换为字符串Laravel [英] Pluck id (integer) cast to string Laravel
问题描述
在从数据库中拔取数据时,我得到 id
作为字符串。
$字母=新字母();
return $ alphabets-> pluck('name','id');
输出
{
1:Apple,
2:Ball,
3 :猫
}
期望
{
1:Apple,
2:Ball,
3:Cat
}
但是,当我将 ID
和 name
,
返回$ alphabets-> pluck('id','name');
我将id设为整数。 我不确定幕后发生了什么。但是,我怎样才能得到整数的ID?实际上,由于
<$ p
$ {
Apple:1,
Ball:2,
Cat:3
}
1与1
在表单集中,旧的Flash会话没有设置值。
{!! Form :: select('alphabet',$ alphabets,null,['class'=>'form-control','multiple'=> true])!!}
我想我在这里找到了答案。
https://laracasts.com/discuss/channels / laravel / pluck-id-integer-cast-to-string
这里我发现JSON只允许键名是字符串。 strong>
{
1:Apple,
2 :球,
3:猫
}
其实,我想为表单集合
实现它。这是一个bug,它现在已经被合并。
https://github.com/LaravelCollective/html/pull/368#pullrequestreview-46820423
While plucking from a database, I get id
as strings.
$alphabets = new Alphabet();
return $alphabets->pluck('name', 'id');
Output
{
"1": "Apple",
"2": "Ball",
"3": "Cat"
}
Expected
{
1: "Apple",
2: "Ball",
3: "Cat"
}
But, when I reverse ID
and name
,
return $alphabets->pluck('id', 'name');
I get id as integer.
{
"Apple": 1,
"Ball": 2,
"Cat": 3
}
I'm not sure what's happening behind the scene. But how can I get ID in integer ? Actually, old flash session doesn't set value because of 1 vs "1"
in Form Collective.
{!! Form::select('alphabet', $alphabets, null, ['class' => 'form-control', 'multiple' => true]) !!}
I think I found the answer here.
https://laracasts.com/discuss/channels/laravel/pluck-id-integer-cast-to-string
Here I found JSON only allows key names to be strings.
Using number as "index" (JSON)
{
"1": "Apple",
"2": "Ball",
"3": "Cat"
}
Actually, I want to achieve it for Form Collective
. It was a bug and it's PR has been merged now.
https://github.com/LaravelCollective/html/pull/368#pullrequestreview-46820423
这篇关于将id(整数)转换为字符串Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!