久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      • <bdo id='fee80'></bdo><ul id='fee80'></ul>

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

    1. <tfoot id='fee80'></tfoot>

    2. <i id='fee80'><tr id='fee80'><dt id='fee80'><q id='fee80'><span id='fee80'><b id='fee80'><form id='fee80'><ins id='fee80'></ins><ul id='fee80'></ul><sub id='fee80'></sub></form><legend id='fee80'></legend><bdo id='fee80'><pre id='fee80'><center id='fee80'></center></pre></bdo></b><th id='fee80'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='fee80'><tfoot id='fee80'></tfoot><dl id='fee80'><fieldset id='fee80'></fieldset></dl></div>
      <legend id='fee80'><style id='fee80'><dir id='fee80'><q id='fee80'></q></dir></style></legend>

      1. Laravel eloquent 模型模型屬性轉(zhuǎn)換(我應(yīng)該轉(zhuǎn)換哪些

        Laravel eloquent model model attribute casting (Which attribute types should I cast?)(Laravel eloquent 模型模型屬性轉(zhuǎn)換(我應(yīng)該轉(zhuǎn)換哪些屬性類型?))
          <bdo id='qnXg1'></bdo><ul id='qnXg1'></ul>
        • <tfoot id='qnXg1'></tfoot>

            1. <i id='qnXg1'><tr id='qnXg1'><dt id='qnXg1'><q id='qnXg1'><span id='qnXg1'><b id='qnXg1'><form id='qnXg1'><ins id='qnXg1'></ins><ul id='qnXg1'></ul><sub id='qnXg1'></sub></form><legend id='qnXg1'></legend><bdo id='qnXg1'><pre id='qnXg1'><center id='qnXg1'></center></pre></bdo></b><th id='qnXg1'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='qnXg1'><tfoot id='qnXg1'></tfoot><dl id='qnXg1'><fieldset id='qnXg1'></fieldset></dl></div>
                    <tbody id='qnXg1'></tbody>

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

                • <legend id='qnXg1'><style id='qnXg1'><dir id='qnXg1'><q id='qnXg1'></q></dir></style></legend>
                • 本文介紹了Laravel eloquent 模型模型屬性轉(zhuǎn)換(我應(yīng)該轉(zhuǎn)換哪些屬性類型?)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  我不確定我是否完全理解 Laravel Eloquent 屬性轉(zhuǎn)換.根據(jù)文檔,(https://laravel.com/docs/8.x/eloquent-mutators#attribute-casting),這些是支持的類型:

                  I am not sure I fully understand Laravel Eloquent attribute casting. According to documentation, (https://laravel.com/docs/8.x/eloquent-mutators#attribute-casting), these are the supported types:

                  整數(shù)、實(shí)數(shù)、浮點(diǎn)數(shù)、雙精度數(shù)、十進(jìn)制數(shù):、字符串、布爾值、對(duì)象、數(shù)組、集合、日期、日期時(shí)間、時(shí)間戳、加密、加密:對(duì)象、加密:數(shù)組和加密:集合

                  integer, real, float, double, decimal:, string, boolean, object, array, collection, date, datetime, timestamp, encrypted, encrypted:object, encrypted:array, and encrypted:collection

                  到目前為止,我只在我的模型上使用了日期轉(zhuǎn)換(當(dāng)字段作為時(shí)間戳存儲(chǔ)在數(shù)據(jù)庫(kù)中時(shí)),如下所示:

                  So far, I've only used date casting on my models (when the fields were stored as timestamps in the db), like this:

                  protected $dates = [
                      'modified_at', 'published_at'
                  ];
                  

                  我也理解當(dāng)值存儲(chǔ)為整數(shù)(0 或其他)時(shí)需要將屬性轉(zhuǎn)換為布爾值.

                  I also understand the need for attribute casting to boolean when the values are stored as integers (0 or sth else).

                  但是對(duì)于其他屬性類型(例如整數(shù)),我應(yīng)該始終進(jìn)行屬性轉(zhuǎn)換嗎?或者只是當(dāng)數(shù)據(jù)庫(kù)中的字段屬于不同類型時(shí)?有哪些用例或其他屬性的最佳實(shí)踐是什么?

                  But what about other attribute types (integers, for example), should I always do attribute casting? Or just when the field in the database is of a different type? What are the use cases or what is the best practice with other attributes?

                  (例如,我無(wú)法想象在遷移中創(chuàng)建一個(gè)字符串字段,然后將其中的一些數(shù)字保存為字符串,然后將其轉(zhuǎn)換回模型上的整數(shù)?)

                  (I can't, for example, imagine creating a string field in migrations, then saving some number inside it as string and then casting it back into an integer on the model?)

                  推薦答案

                  默認(rèn)情況下,屬性將轉(zhuǎn)換為表中定義的列類型.因此,如果您的列是整數(shù),那么它將被轉(zhuǎn)換為 int.

                  By default, attributes will be casted to the type of column defined in the table. So, if your column is an integer, then it will be casted as int.

                  但是,如果您想為特定字段修改此行為,會(huì)發(fā)生什么?這就是屬性轉(zhuǎn)換進(jìn)入場(chǎng)景的時(shí)候.

                  But, what happens if you want to modify this behavior for specific fields? That's when attribute casting enters the scene.

                  例如,假設(shè)我們?cè)诿麨?projects 的表中有一個(gè)名為 config 的列,類型為 json,我們可以在其中存儲(chǔ)其他配置元素每個(gè)項(xiàng)目.

                  For example, imagine we have in a table called projects a column named config of type json in which we can store additional configuration elements for each project.

                  就我而言,能夠?qū)⑦@些數(shù)據(jù)作為 array 處理會(huì)很有用.因此,我們無(wú)需接收stringobject,而只需處理一個(gè)簡(jiǎn)單的array.為此,我們只需:

                  In my case, it'd be useful to be able to handle this data as an array. So, instead of receiving a string or object, we can just deal with a simple array. To do this, we just:

                  class Project extends Model
                  {
                      // ...
                  
                      protected $casts = [
                          'config' => 'array',
                      ];
                  
                      // ...
                  }
                  

                  這樣,每當(dāng)我們使用 Eloquent 從數(shù)據(jù)庫(kù)中獲取項(xiàng)目時(shí),每條記錄都會(huì)將該屬性作為 array.而且,當(dāng)嘗試存儲(chǔ)/更新記錄時(shí),它會(huì)被轉(zhuǎn)換回 json.

                  This way, whenever we use Eloquent to fetch projects from the database, each record will have that property as an array. And also, it will be converted back to json when trying to store/update records.

                  與您指定的情況有關(guān)(將元素保存為 string,然后將其檢索為 integer)當(dāng)然是完全可能的.您需要為該字段設(shè)置訪問(wèn)器和修改器.對(duì)于名為 number 的屬性:

                  Related to the case you specified (saving element as a string but then retrieve it as an integer) is totally possible of course. You'll need to set both the accessor and the mutator for that field. For an attribute named number:

                  /**
                   * This will be called when fetching the element.
                   */
                  public function getNumberAttribute($value)
                  {
                      return (int)$value;
                  }
                  
                  /**
                   * This will be called when storing/updating the element.
                   */
                  public function setFirstNameAttribute($value)
                  {
                      $this->attributes['number'] = (string)$value;
                  }
                  

                  現(xiàn)在,有什么需要這樣做的理由嗎?好吧,您可能正在處理一個(gè)設(shè)計(jì)不當(dāng)?shù)臄?shù)據(jù)庫(kù),或者正在處理多個(gè)系統(tǒng)正在使用的生產(chǎn)數(shù)據(jù)庫(kù)并且數(shù)據(jù)庫(kù)很難完成.在這些情況下,您可以利用這種值操作在您的應(yīng)用中按您的意愿工作.

                  Now, a reason to ever need to make this? Well, you could be dealing with a database not properly well designed, or with a production database that is being used by multiple systems and changes in the db are hard to accomplish. In those cases you could make use of this kind of value manipulation to work as you want in your app.

                  這篇關(guān)于Laravel eloquent 模型模型屬性轉(zhuǎn)換(我應(yīng)該轉(zhuǎn)換哪些屬性類型?)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動(dòng)游標(biāo)不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術(shù)方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個(gè)值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動(dòng)程序)
                        <tbody id='U9juZ'></tbody>

                      <tfoot id='U9juZ'></tfoot>
                        <legend id='U9juZ'><style id='U9juZ'><dir id='U9juZ'><q id='U9juZ'></q></dir></style></legend>
                        <i id='U9juZ'><tr id='U9juZ'><dt id='U9juZ'><q id='U9juZ'><span id='U9juZ'><b id='U9juZ'><form id='U9juZ'><ins id='U9juZ'></ins><ul id='U9juZ'></ul><sub id='U9juZ'></sub></form><legend id='U9juZ'></legend><bdo id='U9juZ'><pre id='U9juZ'><center id='U9juZ'></center></pre></bdo></b><th id='U9juZ'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='U9juZ'><tfoot id='U9juZ'></tfoot><dl id='U9juZ'><fieldset id='U9juZ'></fieldset></dl></div>
                            <bdo id='U9juZ'></bdo><ul id='U9juZ'></ul>

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

                            主站蜘蛛池模板: 久久天堂| 日韩精品在线看 | 亚洲精品日韩综合观看成人91 | 久久久久一区二区三区四区 | 国际精品鲁一鲁一区二区小说 | 91偷拍精品一区二区三区 | 一级毛片在线播放 | xxx.在线观看 | 午夜精品一区二区三区在线观看 | a级大片 | 午夜无码国产理论在线 | 2019天天操 | 欧美成人一级视频 | 黄色香蕉视频在线观看 | 一级国产精品一级国产精品片 | 国产精品亚洲成在人线 | 欧美精品二区 | 久久国内| 天天干天天想 | 日韩综合在线视频 | 亚洲欧美综合 | 日韩在线三级 | 综合一区二区三区 | 成人精品一区二区三区四区 | 99精品久久 | 国产高清在线精品一区二区三区 | 天天噜天天干 | 国产精品视频久久 | 国产免费视频 | 日韩毛片网 | 97精品国产97久久久久久免费 | 91综合网 | 免费视频一区二区 | 在线2区 | 国产999精品久久久久久 | 欧美成人h版在线观看 | 亚洲精品在线免费观看视频 | 国产成人a亚洲精品 | 国产高清精品一区 | 欧美一级特黄aaa大片在线观看 | 在线视频99|