Search code examples
phplaraveleager-loading

Eager loading does not return anything


When I eager load like this:

 return Port::filter($filters)
            ->with(['scores' => function ($query) {
                    $query->select(["*"]);
                }]
            )
            ->actives()
            ->paginate(14);

I get the expected result. However when I select specific column names:

 return Port::filter($filters)
            ->with(['scores' => function ($query) {
                    $query->select(["id, name"]);
                }]
            )
            ->actives()
            ->paginate(14);

Result is always []. What could be wrong here?


Solution

  • In select we need to pass column names separated by comma's

     return Port::filter($filters)
            ->with(['scores' => function ($query) {
                    $query->select("id", "name");
                }]
            )
            ->actives()
            ->paginate(14);