2017-04-07 46 views

回答

2

如果雄辯車型的集合,該車型也將轉換與指定者()

$col->toArray(); 

與所有它將返回雄辯車型的陣列陣列,而不將它們轉換爲數組。

$col->all(); 

的指定者方法集合轉換成純PHP陣列。如果集合的價值觀是口才模型,這些模型也將被轉換爲數組: toArray()

所有()收集

/** 
* Get all of the items in the collection. 
* 
* @return array 
*/ 
public function all() 
{ 
    return $this->items; 
} 

指定者(在返回的項目)返回如果Arrayable將它們轉換爲數組:

/** 
* Get the collection of items as a plain array. 
* 
* @return array 
*/ 
public function toArray() 
{ 
    return array_map(function ($value) { 
     return $value instanceof Arrayable ? $value->toArray() : $value; 
    }, $this->items); 
} 

例如:獲取您的所有用戶數據庫是這樣的:

$users = User::all(); 

然後把它們扔掉,每程,你會看到不同:

dd($users->all()); 

而且隨着指定者()

dd($users->toArray()); 
相關問題