Skip to content

Wrong Eloquent model ID after query with JOIN  #4962

@Grey2k

Description

@Grey2k

In last version of laravel

im using two models:

  1. contact
  2. document, which belongsTo contact by contact_id field

For order by relation field (for example last_name)

im using (may be isnt correct , but not anything about it in documentation)

$documents = Document::with('contact')->join('contacts', 'contacts.id', '=', 'documents.id')->orderBy('contacts.name')->get()

after that query in loop

foreach($documents AS $document){
    if($document->id == $document->contact->id){
        // always matched here!  but in database its not like that
    }
}

if i remove join() - all correct,
please this is a bug or feature and advice about orderBy relation fields.

Thanks

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