Skip to content

Select table.* with first() #229

@djibarian

Description

@djibarian

In the Select * from table section docs say:

...if you call the joinRelationship method without previously selecting any specific columns, Eloquent Power Joins will automatically include that for you.

Imagine this query:

$derivedState = Element::whereKey($elementId)
	->leftJoinRelationship('description')
	->first('styled_texts.text');

which selects the columns through first() instead of using select(). The problem is that the join relationship completely ignores the column selection and inserts a elements.* instead. As a workaround, you have to write:

$derivedState = Element::whereKey($elementId)
	->leftJoinRelationship('description')
	->select('styled_texts.text')
	->first();

But what if you have plenty of first() (or firstOrFail(), or whatever any other non-select that actually selects columns) in the codebase? In that case I see it TOO DANGEROUS to mentally remember that whenever a Power Join is used, you must always use select(), because otherwise the column selection is completely ignored and instead the result inadvertently includes ALL columns, which is something that probably you are trying to avoid in most of the cases, specially if a row contains columns which are for example huge GEOMETRY, TEXT or BLOBs and you are only interested in an id.

Proposal

In my case, I'd be happy if this feature could be completely disabled/removed, and rather prefer to get an error in the case of column ambiguities, because as a rule, never ever reference unqualified columns in a multi-table select, and never ever use * in any select. Also, what's the point of selecting all columns in the main table if you are joining others? Maybe in a normal join would make sense, but in a left join?

That said, I understand that this can't be the general case, so probably the best solution would be for Power Joins to look for column selection more thoroughly, not only via select().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions