<small id='u7wHE'></small><noframes id='u7wHE'>

  • <legend id='u7wHE'><style id='u7wHE'><dir id='u7wHE'><q id='u7wHE'></q></dir></style></legend>

      <tfoot id='u7wHE'></tfoot>

      <i id='u7wHE'><tr id='u7wHE'><dt id='u7wHE'><q id='u7wHE'><span id='u7wHE'><b id='u7wHE'><form id='u7wHE'><ins id='u7wHE'></ins><ul id='u7wHE'></ul><sub id='u7wHE'></sub></form><legend id='u7wHE'></legend><bdo id='u7wHE'><pre id='u7wHE'><center id='u7wHE'></center></pre></bdo></b><th id='u7wHE'></th></span></q></dt></tr></i><div id='u7wHE'><tfoot id='u7wHE'></tfoot><dl id='u7wHE'><fieldset id='u7wHE'></fieldset></dl></div>
          <bdo id='u7wHE'></bdo><ul id='u7wHE'></ul>

        Eloquent 将十进制转换为字符串

        时间:2023-09-24
            <legend id='4Dmjn'><style id='4Dmjn'><dir id='4Dmjn'><q id='4Dmjn'></q></dir></style></legend>
              <i id='4Dmjn'><tr id='4Dmjn'><dt id='4Dmjn'><q id='4Dmjn'><span id='4Dmjn'><b id='4Dmjn'><form id='4Dmjn'><ins id='4Dmjn'></ins><ul id='4Dmjn'></ul><sub id='4Dmjn'></sub></form><legend id='4Dmjn'></legend><bdo id='4Dmjn'><pre id='4Dmjn'><center id='4Dmjn'></center></pre></bdo></b><th id='4Dmjn'></th></span></q></dt></tr></i><div id='4Dmjn'><tfoot id='4Dmjn'></tfoot><dl id='4Dmjn'><fieldset id='4Dmjn'></fieldset></dl></div>
              <tfoot id='4Dmjn'></tfoot>
                <tbody id='4Dmjn'></tbody>

                <small id='4Dmjn'></small><noframes id='4Dmjn'>

                  <bdo id='4Dmjn'></bdo><ul id='4Dmjn'></ul>
                  本文介绍了Eloquent 将十进制转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我的 Eloquent 模型中有一个十进制字段来表示金钱,但在尝试向该字段添加一些数字时,我发现了错误的结果.

                  经过进一步检查,我发现decimal 字段被转换为字符串,就像 tinker 控制台中的 "1245114.00".

                  我检查了表结构,可以验证该字段确实是decimal(11,3).

                  这个问题是之前问过但没有答案.

                  为什么会这样?

                  解决方案

                  您需要在模型中定义哪些字段需要转换为原始属性.

                  protected $casts = ['my_decimal' =>'漂浮',];

                  <块引用>

                  模型上的 $casts 属性提供了一种将属性转换为常见数据类型的便捷方法.$casts 属性应该是一个数组,其中键是要转换的属性的名称,值是您希望将列转换为的类型.支持的转换类型有:integerrealfloatdoublestringbooleanobjectarraycollectiondatedatetime时间戳

                  这里有一个非常好的解释:

                  https://mattstauffer.com/blog/laravel-5.0-eloquent-attribute-casting/

                  文档中也有解释:

                  https://laravel.com/docs/5.5/eloquent-mutators#属性转换

                  I have a decimal field to represent money in my Eloquent Model and I'm noticing erroneous results when trying to add some number to this field.

                  Upon further inspection, I found that the decimal field is being cast as a string, like "1245114.00" in the tinker console.

                  I checked the table structure and I can verify that the field is indeed decimal(11,3).

                  This question was asked before but has no answers.

                  Why is this happening?

                  解决方案

                  You need to define in your model which fields need to be cast to a primitive attribute.

                  protected $casts = [
                      'my_decimal' => 'float',
                  ];
                  

                  The $casts property on your model provides a convenient method of converting attributes to common data types. The $casts property should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to. The supported cast types are: integer, real, float, double, string, boolean, object, array, collection, date, datetime, and timestamp

                  There is a really good explanation here:

                  https://mattstauffer.com/blog/laravel-5.0-eloquent-attribute-casting/

                  Also there is an explanation in the docs:

                  https://laravel.com/docs/5.5/eloquent-mutators#attribute-casting

                  这篇关于Eloquent 将十进制转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel - 使用 Eloquent 查询构建器在选择中添加自定 下一篇:Laravel eloquent 集合上的 Array_unique

                  相关文章

                  最新文章

                • <tfoot id='bismU'></tfoot>

                    <i id='bismU'><tr id='bismU'><dt id='bismU'><q id='bismU'><span id='bismU'><b id='bismU'><form id='bismU'><ins id='bismU'></ins><ul id='bismU'></ul><sub id='bismU'></sub></form><legend id='bismU'></legend><bdo id='bismU'><pre id='bismU'><center id='bismU'></center></pre></bdo></b><th id='bismU'></th></span></q></dt></tr></i><div id='bismU'><tfoot id='bismU'></tfoot><dl id='bismU'><fieldset id='bismU'></fieldset></dl></div>
                    • <bdo id='bismU'></bdo><ul id='bismU'></ul>

                    <small id='bismU'></small><noframes id='bismU'>

                      <legend id='bismU'><style id='bismU'><dir id='bismU'><q id='bismU'></q></dir></style></legend>