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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 系列化json ### 介紹 構建JSON API時,通常需要將模型和關系轉換為數組或JSON。模型包括進行這些轉換以及控制序列化中包含哪些屬性的便捷方法。 ### [](https://octobercms.com/docs/database/serialization#basic-usage)基本用法 #### 將模型轉換為數組 要將模型及其加載的[關系](https://octobercms.com/docs/database/relations)轉換為數組,可以使用`toArray`方法。此方法是遞歸的,因此所有屬性和所有關系(包括關系的關系)都將轉換為數組: ~~~ $user = User::with('roles')->first(); return $user->toArray(); ~~~ 您也可以將[集合](https://octobercms.com/docs/database/collections)轉換為數組: ~~~ $users = User::all(); return $users->toArray(); ~~~ #### 將模型轉換為JSON 要將模型轉換為JSON,可以使用`toJson`方法。像一樣`toArray`,該`toJson`方法是遞歸的,因此所有屬性和關系都將轉換為JSON: ~~~ $user = User::find(1); return $user->toJson(); ~~~ 或者,您可以將模型或集合轉換為字符串,這將自動調用該`toJson`方法: ~~~ $user = User::find(1); return (string) $user; ~~~ 由于模型和集合在轉換為字符串時會轉換為JSON,因此您可以直接從應用程序的路由,AJAX處理程序或控制器返回Model對象: ~~~ Route::get('users', function () { return User::all(); }); ~~~ ### [](https://octobercms.com/docs/database/serialization#hiding-attributes-from-json)從JSON隱藏屬性 有時您可能希望限制模型數組或JSON表示形式中包含的屬性(例如密碼)。為此,`$hidden`向模型添加屬性定義: ~~~ <?php namespace Acme\Blog\Models; use Model; class User extends Model { /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = ['password']; } ~~~ 或者,您可以使用`$visible`屬性定義應包含在模型的數組和JSON表示形式中的屬性白名單: ~~~ class User extends Model { /** * The attributes that should be visible in arrays. * * @var array */ protected $visible = ['first_name', 'last_name']; } ~~~ ### [](https://octobercms.com/docs/database/serialization#appending-values-to-json)將值附加到JSON 有時,您可能需要添加數據庫中沒有對應列的數組屬性。為此,首先為該值定義一個[訪問器](https://octobercms.com/docs/database/mutators): ~~~ class User extends Model { /** * Get the administrator flag for the user. * * @return bool */ public function getIsAdminAttribute() { return $this->attributes['admin'] == 'yes'; } } ~~~ 創建訪問器后,將屬性名稱添加到`appends`模型的屬性中: ~~~ class User extends Model { /** * The accessors to append to the model's array form. * * @var array */ 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>

                              哎呀哎呀视频在线观看