Alias id to _id in the query builder

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Fixed
    • Priority: Unknown
    • laravel-5.0.0
    • Affects Version/s: None
    • Component/s: Laravel
    • None
    • PHP Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      By convention in Eloquent, model's primary key is id. But MongoDB uses _id as primary key (unique index automatically created and can't be removed).

       

      In order to support Laravel packages out of the box, the proposition is to modify the query builder so any query on the "id" field is converted to a query to "_id" field.

      $builder->where('id', 123)

      becomes

      { _id: 123 }

      And the same for the result. When _id is returned, the value is converted to id. This is the most tricky part as we don't want to break backward compatibility for projects that refer to _id. But this is a necessary abstraction to match Eloquent design.

      Breaking change: using an "id" field distinct from "_id" would not be supported.

      Related rejected PR:

            Assignee:
            Jérôme Tamarelle
            Reporter:
            Jérôme Tamarelle
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: