2017-04-19 30 views
0

我一直在嘗試使用Laravel 5作爲Spring引導設置的替代方法。這兩行代碼在抓取相同數據時如何提供不同的集合集?

$employees = App\Employee::all(); 

$employees = DB::table('employees')->get(); 

我讀了那個的這兩行代碼做同樣的事情,從我的「僱員」表中檢索的所有記錄。這是真實的在一定程度上,但是第一行中,雖然更加簡潔和乾淨,提供代表相同數量的記錄

Employee {#180 ▼ 
     #connection: null 
     #table: null 
     #primaryKey: "id" 
     #keyType: "int" 
     +incrementing: true 
     #with: [] 
     #perPage: 15 
     +exists: true 
     +wasRecentlyCreated: false 
     #attributes: array:3 [▶] 
     #original: array:3 [▶] 
     #casts: [] 
     #dates: [] 
     #dateFormat: null 
     #appends: [] 
     #events: [] 
     #observables: [] 
     #relations: [] 
     #touches: [] 
     +timestamps: true 
     #hidden: [] 
     #visible: [] 
     #fillable: [] 
     #guarded: array:1 [▶] 
    } 

一個更詳細的和巢狀收集VS

{#173 ▼ 
     +"id": 3 
     +"username": "deren" 
     +"password": "noob" 
    } 

對於第一收集,第二集合的相同信息包含在原始內容中。兩者之間有什麼區別,爲什麼通過Eloquent的第一個查詢包含更多的元數據?我覺得它與它提供的口才(赦免雙關語)相矛盾,特別是考慮到我不知道如何訪問原始數組中的數組。我使用dd()來轉儲包含集合的變量,如果這有所作爲。

回答

2
$employees = App\Employee::all(); 

這讀取數據庫表,變成每一行成特定的模式類的一個實例,並與他們所有的附加屬性,方法,等返回他們..

$employees = DB::table('employees')->get(); 

這讀取數據庫表並將每一行變成一個標準的類。它不瞭解模型。

相關問題