Framework: Eager loading specific columns does not work when there is space between each column

Created on 31 Jan 2018  路  5Comments  路  Source: laravel/framework

This would work.
$users = App\Book::with('author:id,name,email')->get();

This would not.
$users = App\Book::with('author:id, name, email')->get();

It gives this error.
Unknown column ' name' in 'field list'

I think the space is detected as part of the table's name.

Most helpful comment

So follow the documentation and don't use spaces? 馃槒

All 5 comments

So follow the documentation and don't use spaces? 馃槒

Yeah, but I like spaces between my columns.

That's how it works currently, sorry.

This is so strange, I don't have spaces, but Laravel adds a space for some reason and then complains about it!

@json(\App\Language::with('subjects:id,title')->get())
SQLSTATE[42S22]: Column not found: 1054 Unknown column ' title' in 'field list' (SQL: select `id`, ` title` from `subjects` where `subjects`.`language_id` in (1, 3, 4, 5) and `subjects`.`deleted_at` is null)

The same thing is happening to me as @mokhosh :

@json(\App\Models\TagSets::with('tags:id,name,color')->get());

I get this error: Unknown column ' name' in 'field list'

Anyone else experienced this and how to fix?

Update:

I realized that the similarity in both our cases is the @json directive. For anyone in the future who experiences this, the fix is simply to put it outside of the @json directive.

@php
    $tagSets = \App\Models\TagSets::with('tags:id,name,color')->get();
@endphp

@json($tagSets);

So it seems the @json is putting it off. Could this be a bug worth looking into @themsaid ? Frankly the workaround is not that big of a pain though :)

Was this page helpful?
0 / 5 - 0 ratings