I want to make some changes to
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
but I don't want to make changes to the vendor.
public function match(array $models, Collection $results, $relation)
{
    $foreign = $this->foreignKey;
    $owner = $this->ownerKey;
    // First we will get to build a dictionary of the child models by their primary
    // key of the relationship, then we can easily match the children back onto
    // the parents using that dictionary and the primary key of the children.
    $dictionary = [];
    foreach ($results as $result) {
        $dictionary[$result->getAttribute($owner)] = $result;
    }
    // Once we have the dictionary constructed, we can loop through all the parents
    // and match back onto their children using these keys of the dictionary and
    // the primary key of the children to map them onto the correct instances.
    foreach ($models as $model) {
        // Add this block //////////////////////////////////////////
        if( isset($model->{$foreign}->oid)){
            if (isset($dictionary[$model->{$foreign}->oid])) {
                $model->setRelation($relation, $dictionary[$model->{$foreign}->oid]);
            }
        }else {
        // End of block ////////////////////////////////////////////
            if (isset($dictionary[$model->{$foreign}])) {
                $model->setRelation($relation, $dictionary[$model->{$foreign}]);
            }
        }
    }
    return $models;
}
It is the only way to make jenssegers/laravel-mongodb to work with ObjectId relations
via Chebli Mohamed
 
Aucun commentaire:
Enregistrer un commentaire