jeudi 29 décembre 2016

Get relation for multiple objects with laravel

A User has many Phones. I want to get all the phones from active users. I can do:

$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.

But I'm not sure it's the more elegant or laravel-ish way. Is there a built in magic function for such a case? Can I get all the phones from active users without writing a loop and insert in an Array?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2hzlwgx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire