Matth__
Matth__

Reputation: 43

Eloquent hasmany relationship issue

I am building an API with the lumen micro-framework using Eloquent for the database.

When i use this code to get the shoppinglists from a group, everything works

$group =  Group::with(['shoppingLists' => function($query) {
        }])->get();

It returns the following object

{
        "id": "797799c2-6044-4a3a-a3a6-a71fbb17de68",
        "name": "'t snackske",
        "description": "best group ever",
        "user_id": "7e74223a-ea06-46bf-ab1a-abb01b287e32",
        "created_at": "2015-08-09 20:06:40",
        "updated_at": "2015-08-09 20:06:40",
        "shopping_lists": [
            {
                "id": "2423eb3c-7dab-4672-b382-895788dec6a0",
                "name": "shop",
                "description": "food",
                "user_id": "7e74223a-ea06-46bf-ab1a-abb01b287e32",
                "group_id": "797799c2-6044-4a3a-a3a6-a71fbb17de68",
                "created_at": "2015-08-09 20:06:40",
                "updated_at": "2015-08-09 20:06:40"
            }
        ]
}

And when I check the queries that are logged i get the following queries:

{
    "query": "select * from `groups`",
    "bindings": [],
    "time": 0.31
},
{
    "query": "select * from `shopping_lists` where `shopping_lists`.`group_id` in (?)",
    "bindings": [
        "797799c2-6044-4a3a-a3a6-a71fbb17de68"
    ],
    "time": 0.34
}

But when i try select specific fields for the shoppinglists this query returns no records The query builder:

$group =  Group::with(['shoppingLists' => function($query) {
            $query->addSelect('id', 'name', 'description');
        }])->get();

The response:

{
        "id": "797799c2-6044-4a3a-a3a6-a71fbb17de68",
        "name": "Group",
        "description": "testgroup",
        "user_id": "7e74223a-ea06-46bf-ab1a-abb01b287e32",
        "created_at": "2015-08-09 20:06:40",
        "updated_at": "2015-08-09 20:06:40",
        "shopping_lists": []
}

The queries from the getQueryLog function

{
    "query": "select * from `groups`",
    "bindings": [],
    "time": 0.23
},
{
    "query": "select `id`, `name`, `description` from `shopping_lists` where `shopping_lists`.`group_id` in (?)",
    "bindings": [
        "797799c2-6044-4a3a-a3a6-a71fbb17de68"
    ],
    "time": 0.32
}

When i copy and paste the last query from the query log in mysql i get the correct row. For some reason, eloquent doesn't show the data from the shopping_lists.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Group extends Model
{
    protected $increments = false;

    protected $fillable = [
        'id',
        'name',
        'description',
        'user_id'
    ];

    protected $primaryKey = 'id';

    /**
     * Get shoppinglists from this group
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function shoppingLists()
    {
        return $this->hasMany('App\ShoppingList', 'group_id', 'id');
    }
}

Upvotes: 2

Views: 95

Answers (1)

Jeemusu
Jeemusu

Reputation: 10533

You may have to add the foreign key column to the addSelect() method for Laravel to do it's magic.

Group::with(['shoppingLists' => function($query) {
    $query->addSelect('id', 'group_id', 'name', 'description');
}])->get();

Upvotes: 2

Related Questions