specialtactics / laravel-api-boilerplate

Laravel API Boilerplate | Quickly build quality API products!
MIT License
497 stars 96 forks source link

[BUG] 'id' in response got unset if $primaryKey in Model set to 'id' #26

Closed JT501 closed 5 years ago

JT501 commented 5 years ago

Version Information PHP Version: 7.3 Package Version (l5-api): 1.0.3

Describe the bug The Primary got unset in response.

To Reproduce Steps to reproduce the behavior: Set $primaryKey = 'id' in Model. Return response using BaseTransformer.

Expected behavior Data with primary key.

Additional context nope

I managed to find out the bug in RestfulTransformer.php.

 /**
  * Primary Key transformation - all PKs to be called "id"
  */
 $transformed = array_merge(
        ['id' => $model->getKey()],
        $transformed
);
unset($transformed[$model->getKeyName()]);

I added if condition to check if the primary key equal to 'id' or not, before the unset action.

 /**
  * Primary Key transformation - all PKs to be called "id"
  */
 $transformed = array_merge(
        ['id' => $model->getKey()],
        $transformed
);
if ($model->getKeyName() != 'id') {
        unset($transformed[$model->getKeyName()]);
}
JT501 commented 5 years ago

I have submitted a pull request to l5-api

specialtactics commented 5 years ago

Thanks for reporting! I will check out the PR

specialtactics commented 5 years ago

Merged, thanks !