<bdo id='jmXtE'></bdo><ul id='jmXtE'></ul>

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

    1. <legend id='jmXtE'><style id='jmXtE'><dir id='jmXtE'><q id='jmXtE'></q></dir></style></legend>

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

      Laravel Eloquent Serialization:如何重命名属性?

      时间:2023-09-23

      1. <i id='ViDY5'><tr id='ViDY5'><dt id='ViDY5'><q id='ViDY5'><span id='ViDY5'><b id='ViDY5'><form id='ViDY5'><ins id='ViDY5'></ins><ul id='ViDY5'></ul><sub id='ViDY5'></sub></form><legend id='ViDY5'></legend><bdo id='ViDY5'><pre id='ViDY5'><center id='ViDY5'></center></pre></bdo></b><th id='ViDY5'></th></span></q></dt></tr></i><div id='ViDY5'><tfoot id='ViDY5'></tfoot><dl id='ViDY5'><fieldset id='ViDY5'></fieldset></dl></div>
      2. <small id='ViDY5'></small><noframes id='ViDY5'>

        <tfoot id='ViDY5'></tfoot>
      3. <legend id='ViDY5'><style id='ViDY5'><dir id='ViDY5'><q id='ViDY5'></q></dir></style></legend>

                <bdo id='ViDY5'></bdo><ul id='ViDY5'></ul>
                  <tbody id='ViDY5'></tbody>
              • 本文介绍了Laravel Eloquent Serialization:如何重命名属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                例如,我有扩展 Eloquent 的用户模型.在数据库表中,列名是user_id.

                For example, I have User model extending Eloquent. In the database table, the column name is user_id.

                阅读后如何将结果输出为'userId'?

                How do I output the result as 'userId' after reading?

                推荐答案

                使用属性访问器添加单个别名"

                您可以使用属性访问器来创建新属性":

                public function getUserIdAttribute(){
                    return $this->attributes['user_id'];
                }
                

                这允许您以这种方式访问​​值:$user->userId

                This allows you to access the value this way: $user->userId

                现在让我们将值添加到数组/JSON 转换中:

                Now let's add the value to array / JSON conversion:

                protected $appends = array('userId');
                

                最后隐藏丑陋的user_id:

                protected $hidden = array('user_id');
                


                在将模型转换为数组或 JSON 字符串时,您还可以使用 toArray() 更改所有属性名称.

                You can also use toArray() to change the all attribute names when converting the model into an array or JSON string.

                public function toArray(){
                    $array = parent::toArray();
                    $camelArray = array();
                    foreach($array as $name => $value){
                        $camelArray[camel_case($name)] = $value;
                    }
                    return $camelArray;
                }
                

                这篇关于Laravel Eloquent Serialization:如何重命名属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:laravel 挂钩到 Eloquent 前和后保存每个模型 下一篇:获取所有属性的 Laravel 模型

                相关文章

                最新文章

                <small id='83UKA'></small><noframes id='83UKA'>

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

                  <bdo id='83UKA'></bdo><ul id='83UKA'></ul>