[TOC]
### **1、簡介**
當構建?[JSON](http://laravelacademy.org/tags/json "View all posts in JSON")?API 時,經常需要轉化[模型](http://laravelacademy.org/tags/%e6%a8%a1%e5%9e%8b "View all posts in 模型")和關聯關系為[數組](http://laravelacademy.org/tags/%e6%95%b0%e7%bb%84 "View all posts in 數組")或 JSON。[Eloquent](http://laravelacademy.org/tags/eloquent "View all posts in Eloquent")?包含便捷方法實現這些轉換,以及控制哪些屬性被包含到[序列化](http://laravelacademy.org/tags/%e5%ba%8f%e5%88%97%e5%8c%96 "View all posts in 序列化")中。
### **2、基本使用**
#### **轉化模型為數組**
要轉化模型及其加載的關聯關系為數組,可以使用?`toArray`?方法。這個方法是遞歸的,所以所有屬性及其關聯對象屬性(包括關聯的關聯)都會被轉化為數組:
~~~
$user = App\User::with('roles')->first();
return $user->toArray();
~~~
還可以轉化[集合](http://laravelacademy.org/post/144.html)為數組:
~~~
$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();
});
~~~
### **3、在 JSON 中[隱藏屬性](http://laravelacademy.org/tags/%e9%9a%90%e8%97%8f%e5%b1%9e%e6%80%a7 "View all posts in 隱藏屬性")**
有時候你希望在模型數組或 JSON 顯示中隱藏某些屬性,比如密碼,要實現這個,在定義模型的時候設置?`$hidden`?屬性:
~~~
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model{
/**
* 在數組中隱藏的屬性
*
* @var array
*/
protected $hidden = ['password'];
}
~~~
> 注意:如果要隱藏關聯關系,使用關聯關系的方法名,而不是動態屬性名。
此外,可以使用?`visible`?屬性來定義模型數組和 JSON 顯示的屬性白名單:
~~~
<?php
namespace?App;
use?Illuminate\Database\Eloquent\Model;
class?User?extends?Model{
/**
*?在數組中顯示的屬性
*
*?@var?array
*/
protected?$visible?=?['first_name',?'last_name'];
}
~~~
#### **臨時暴露隱藏屬性**
如果你想要在特定模型中臨時顯示隱藏的屬性,可以使用?`makeVisible`?方法,該方法以方法鏈的方式返回模型實例:
~~~
return?$user->makeVisible('attribute')->toArray();
~~~
### **4、追加值到 JSON**
有時候,需要添加數據庫中沒有的字段到數組中,要實現這個功能,首先要為這個值定義一個[訪問器](http://laravelacademy.org/post/3046.html#ipt_kb_toc_3046_1):
~~~
<?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`?設置。
- 序言
- 發行版本說明
- 升級指南
- 貢獻代碼
- 開始
- 安裝
- 配置
- Laravel Homestead
- 基礎
- HTTP 路由
- HTTP 中間件
- HTTP 控制器
- HTTP 請求
- HTTP 響應
- 視圖
- Blade 模板引擎
- 架構
- 一次請求的生命周期
- 應用目錄結構
- 服務提供者
- 服務容器
- 門面(Facades)
- 數據庫
- 起步
- 查詢構建器
- 遷移
- 填充數據
- Eloquent ORM
- 起步
- 關聯關系
- 集合
- 訪問器&修改器
- 序列化
- 服務
- 用戶認證
- 用戶授權
- Artisan Console
- 訂閱支付實現:Laravel Cashier
- 緩存
- 集合
- 集成前端資源:Laravel Elixir
- 加密
- 錯誤&日志
- 事件
- 文件系統/云存儲
- 哈希
- 輔助函數
- 本地化
- 郵件
- 包開發
- 分頁
- Redis
- 隊列
- Session
- Envoy Task Runner
- 任務調度
- 測試
- 驗證
- 新手入門指南
- 簡單任務管理系統
- 帶用戶功能的任務管理系統