<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國際加速解決方案。 廣告
                * * * * * [TOC] ## 簡介 當你在創建 JSON API 的時候,經常會需要將模型和關聯轉換成數組或 JSON。Eloquent 提供了一些便捷的方法來讓我們可以完成這些轉換,以及控制哪些屬性需要被包括在序列化中。 ## 序列化模型 & 集合 ### 序列化成數組 如果要將模型還有其加載的?[關聯](https://laravel-china.org/docs/laravel/5.4/eloquent-relationships)?轉換成一個數組,則可以使用 toArray 方法。這個方法是遞歸的,因此,所有屬性和關聯(包含關聯中的關聯)都會被轉換成數組: ~~~ $user = App\User::with('roles')->first(); return $user->toArray(); ~~~ 你也可以將整個?[集合](https://laravel-china.org/docs/laravel/5.4/eloquent-collections)?轉換成數組: ~~~ $users = App\User::all(); return $users->toArray(); ~~~ ### 序列化成 JSON 如果要將模型轉換成 JSON,則可以使用?`toJson`?方法。如同?`toArray`?方法一樣,`toJson`?方法也是遞歸的。因此,所有的屬性以及關聯都會被轉換成 JSON: ~~~ $user = App\User::find(1); return $user->toJson(); ~~~ 或者,你也可以強制把一個模型或集合轉型成一個字符串,它將會自動調用?`toJson`?方法: ~~~ $user = App\User::find(1); return (string) $user; ~~~ 當模型或集合被轉型成字符串時,模型或集合便會被轉換成?`JSON`?格式,因此你可以直接從應用程序的路由或者控制器中返回?`Eloquent`?對象: ~~~ Route::get('users', function () { return App\User::all(); }); ~~~ ## 隱藏來自 JSON 的屬性 有時候你可能會想要限制包含在模型數組或 JSON 表示中的屬性,比如說密碼。則可以通過在模型中增加?`$hidden`?屬性定義來實現: ~~~ <?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { /** * 在數組中想要隱藏的屬性。 * * @var array */ protected $hidden = ['password']; } ~~~ > {note} 當你要對關聯進行隱藏時,需使用關聯的?**方法**?名稱,而不是它的動態屬性名稱。 另外,你也可以使用?`visible`?屬性來定義應該包含在你的模型數組和 JSON 表示中的屬性白名單。白名單外的其他屬性將隱藏,不會出現在轉換后的數組或 JSON 中: ~~~ <?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { /** * 在數組中可見的屬性。 * * @var array */ protected $visible = ['first_name', 'last_name']; } ~~~ #### 臨時修改屬性的可見度 你可以在模型實例后使用?`makeVisible`?方法來顯示通常隱藏的屬性,且為了便于使用,`makeVisible`?方法會返回一個模型實例: ~~~ return $user->makeVisible('attribute')->toArray(); ~~~ 相應的,你可以在模型實例后使用?`makeHidden`?方法來隱藏通常顯示的屬性: ~~~ return $user->makeHidden('attribute')->toArray(); ~~~ ## 添加參數到 JSON 中 有時候,在轉換模型到 數組 或 JSON 時,你希望添加一個在數據庫中沒有對應字段的屬性。首先你需要為這個值定義一個?[訪問器](https://laravel-china.org/docs/laravel/5.4/eloquent-mutators)?: ~~~ <?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { /** * 為用戶獲取管理者的標記。 * * @return bool */ public function getIsAdminAttribute() { return $this->attributes['admin'] == 'yes'; } } ~~~ 訪問器創建成功后,只需添加該屬性到改模型的?`appends`?屬性中。注意,屬性名稱通常遵循 「[Snake Case](https://en.wikipedia.org/wiki/Snake_case)」 的命名方式,即是訪問器的名稱是基于 「[Camel Case](https://en.wikipedia.org/wiki/Camel_case)」 的命名方式。 ~~~ <?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { /** * 訪問器被附加到模型數組的形式。 * * @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>

                              哎呀哎呀视频在线观看