<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Eloquent ORM —— 序列化 ## 1、簡介 當構建JSON中。 ## 2、基本使用 ### 2.1 轉化模型為數組 要轉化模型及其加載的[關聯關系](http://laravelacademy.org/post/140.html)為數組,可以使用`toArray`方法。這個方法是遞歸的,所以所有屬性及其關聯對象屬性(包括關聯的關聯)都會被轉化為數組: ~~~ $user = App\User::with('roles')->first(); return $user->toArray(); ~~~ 還可以轉化[集合](http://laravelacademy.org/post/144.html)為數組: ~~~ $users = App\User::all(); return $users->toArray(); ~~~ ### 2.2 轉化模型為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(); }); ~~~ ## 3、在JSON中隱藏屬性顯示 有時候你希望在模型數組或JSON顯示中限制某些屬性,比如密碼,要實現這個,在定義模型的時候添加一個`$hidden`屬性: ~~~ <?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model{ /** * 在數組中隱藏的屬性 * * @var array */ protected $hidden = ['password']; } ~~~ > 注意:如果要隱藏關聯關系,使用關聯關系的方法名,而不是動態屬性名。 此外,可以使用`visible`屬性定義屬性顯示的白名單: ~~~ <?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model{ /** * 在數組中顯示的屬性 * * @var array */ protected $visible = ['first_name', 'last_name']; } ~~~ ## 4、追加值到JSON 有時候,需要添加數據庫中沒有相應的字段到數組中,要實現這個,首先要定義一個[訪問器](http://laravelacademy.org/post/146.html#ipt_kb_toc_146_2): ~~~ <?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model{ /** * 為用戶獲取管理員標識 * * @return bool */ public function getIsAdminAttribute() { return $this->attributes['admin'] == 'yes'; } } ~~~ 定義好訪問器后,添加字段名到模型的`appends`屬性: ~~~ <?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>

                              哎呀哎呀视频在线观看