mercredi 5 septembre 2018

List all users and their recipes

I'm writing a custom database query in Laravel for my cocktail app. The goal is to retrieve the following structure:

  • User 1
    • Recipe 1
    • Recipe 4
  • User 2
    • Recipe 2
    • Recipe 3

etc...

My code (see below) currently returns 3 objects, all containing a single recipe but all from 2 users. Is it even possible to build the desired structure (see above), retrieving a user and having his/her recipes stored in a array/object called recipes?

$recipes = DB::table('users')
->join('recipes', 'users.id', '=', 'recipes.user_id')
->select(
    'recipes.id as recipe_id',
    'recipes.name as recipe_name',
    'recipes.short as short',
    'users.name as user_name'
)
->get();



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

Aucun commentaire:

Enregistrer un commentaire