【问题标题】:Pluck id (integer) cast to string Laravel将 id(整数)转换为字符串 Laravel
【发布时间】:2017-12-01 16:39:11
【问题描述】:

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

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

输出

{
    "1": "Apple",
    "2": "Ball",
    "3": "Cat"
}

预期

{
    1: "Apple",
    2: "Ball",
    3: "Cat"
}

但是,当我反转IDname 时,

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

我将 id 设为整数。

{
    "Apple": 1,
    "Ball": 2,
    "Cat": 3
}

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

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

【问题讨论】:

    标签: php json forms laravel pluck


    【解决方案1】:

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

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

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

    Using number as "index" (JSON)

    {
        "1": "Apple",
        "2": "Ball",
        "3": "Cat"
    }
    

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

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

    【讨论】:

    • 当从数据始终以 int 形式出现的数据透视表中提取值时,我们该怎么做?
    【解决方案2】:

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

    || 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;
    }
    

    【讨论】:

      【解决方案3】:

      试试这个代码

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

      Alphabet.php

      你应该像这样投射你的列。

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

      【讨论】:

        【解决方案4】:

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

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

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

        这将为您提供以下结果:

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

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

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

        【讨论】:

        • 这是我的问题的第二种情况,但它不是预期的结果。
        【解决方案5】:

        您还可以将 key 转换为 int

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

        【讨论】:

        • 我也试过了,但结果是一样的。发送
        猜你喜欢
        • 1970-01-01
        • 2015-03-21
        • 2012-02-21
        • 2010-12-31
        相关资源
        最近更新 更多