<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 將模型數據轉成數組 當構建 `JSON API` 時,您可能常常需要把模型和關聯對象轉換成數組或`JSON`。所以Eloquent里已經包含了這些方法。要把模型和已載入的關聯對象轉成數組,可以使用 `toArray `方法: ~~~ $user = User::with('roles')->first(); return $user->toArray(); ~~~ > 注意:也可以把整個的模型集合轉換成數組: ~~~ return User::all()->toArray(); ~~~ ## 將模型轉換成 JSON 要把模型轉換成 JSON,可以使用 toJson 方法: ~~~ return User::find(1)->toJson(); ~~~ ## 從路由中返回模型 注意當模型或集合被轉換成字符串類型時會自動轉換成 JSON 格式,這意味著您可以直接從路由返回 Eloquent 對象! ~~~ Route::get('users', function() { return User::all(); }); ~~~ ## 轉換成數組或 JSON 時隱藏屬性 有時您可能想要限制能出現在數組或 JSON 格式的屬性數據,比如密碼字段。只要在模型里增加 hidden 屬性即可 ~~~ class User extends Model { protected $hidden = ['password']; } ~~~ > 注意: 要隱藏關聯數據,要使用關聯的方法名稱,而不是動態獲取的屬性名稱。 此外,可以使用 visible 屬性定義白名單: ~~~ protected $visible = ['first_name', 'last_name']; ~~~ 有時候您可能想要增加不存在數據庫字段的屬性數據。這時候只要定義一個獲取器即可: ~~~ public function getIsAdminAttribute() { return $this->attributes['admin'] == 'yes'; } ~~~ 定義好獲取器之后,再把對應的屬性名稱加到模型里的 appends 屬性: ~~~ protected $appends = ['is_admin']; ~~~ 把屬性加到 `appends` 數組之后,在模型數據轉換成數組或 `JSON `格式時就會有對應的值。在 `appends `數組中定義的值同樣遵循模型中 `visible `和 `hidden `的設定。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看