1
A User
有許多Phone
s。我想從active
用戶那裏獲得所有手機。我可以這樣做:用laravel獲取多個對象的關係
$phones = [];
$users = User::with('phones')->where('active', 1)->get();
foreach($users as $user) {
$phones = array_merge($phones, $user->phones->toArray());
}
dd($phones); // <- Here are all the phones.
但我不知道它的更優雅或laravel十歲上下的方式。這種情況是否有內置的魔術功能?我可以從活動用戶獲取所有電話而無需編寫循環並插入數組中?
你是太快。 :p –
你需要調用get(); –
謝謝! (我可以在幾分鐘內接受你的答案) –