Pluck id(整数)转换为字符串 Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44800381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:13:41  来源:igfitidea点击:

Pluck id (integer) cast to string Laravel

phpjsonformslaravelpluck

提问by Sagar Chamling

While plucking from a database, I get idas strings.

从数据库中提取时,我得到id字符串。

$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 IDand name,

但是,当我反转ID和时name

return $alphabets->pluck('id', 'name');

I get id as integer.

我得到 id 作为整数。

{
    "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.

我不确定幕后发生了什么。但是我怎样才能得到整数 ID?实际上,由于1 vs "1"在 Form Collective 中,旧的 Flash 会话没有设置值。

{!! Form::select('alphabet', $alphabets, null, ['class' => 'form-control', 'multiple' => true]) !!}

采纳答案by Sagar Chamling

I think I found the answer here.

我想我在这里找到了答案。

https://laracasts.com/discuss/channels/laravel/pluck-id-integer-cast-to-string

https://laracasts.com/discuss/channels/laravel/pluck-id-integer-cast-to-string

Here I found JSON only allows key names to be strings.

在这里我发现 JSON 只允许键名是字符串。

Using number as "index" (JSON)

使用数字作为“索引”(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.

实际上,我想为Form Collective. 这是一个错误,现在它的 PR 已经合并。

https://github.com/LaravelCollective/html/pull/368#pullrequestreview-46820423

https://github.com/LaravelCollective/html/pull/368#pullrequestreview-46820423

回答by Dharmesh Rakholia

Try this code

试试这个代码

$alphabets = new Alphabet();
return $alphabets->all()->pluck('name', 'id');

Alphabet.php

字母表.php

You should cast your columns like this.

你应该像这样投射你的专栏。

  protected $casts = [
    'id' => 'integer',
    'name' => 'string' 
  ];

回答by Shailesh Ladumor

you also convert key into int

您还将密钥转换为 int

 $alphabets = new Alphabet();
    $alphaArr =$alphabets->pluck('name', 'id');
    foreach($array as $key => $value) {
       $newArray[(int) $key] = $value;
    }

回答by Sagar Chamling

Adding this line fix the old session issue of LaravelCollective/Html.

添加此行修复了 LaravelCollective/Html 的旧会话问题。

|| in_array((string) $value, $selected, true)

|| in_array((string) $value, $selected, true)

/**
 * Determine if the value is selected.
 *
 * @param  string $value
 * @param  string $selected
 *
 * @return null|string
 */
protected function getSelectedValue($value, $selected)
{
    if (is_array($selected)) {
        return in_array($value, $selected, true) || in_array((string) $value, $selected, true) ? 'selected' : null;
    } elseif ($selected instanceof Collection) {
        return $selected->contains($value) ? 'selected' : null;
    }
    return ((string) $value == (string) $selected) ? 'selected' : null;
}

回答by Sagar Gautam

Usually, pluck()method gives you associative array of values in string values.

通常,pluck()方法为您提供字符串值中的值的关联数组。

So, try using selectstatements like this:

所以,尝试使用这样的select语句:

$data = Alphabet::select('id','name')->get()->toArray();

This will give you following result:

这将为您提供以下结果:

array:3 [▼
  0 => array:2 [▼
    "id" => 1
    "name" => "Apple"
  ]
  1 => array:2 [▼
    "id" => 2
    "name" => "Ball"
  ]
  2 => array:2 [▼
    "id" => 3
    "name" => "Cat"
  ]
]

Now, using simple loop you can get your expected array.

现在,使用简单的循环,您可以获得预期的数组。

$expected = array();

foreach($data as $d){
    $expected[$d['name']] = $d['id'];
}

dd($expected);