Skip to content

Commit 2cdefe7

Browse files
committed
Merge branch '0.5.1-dev'
2 parents f8523a6 + 0c5e5c3 commit 2cdefe7

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Install the package through composer by adding it to your `composer.json` file:
1111

1212
```
1313
"require": {
14-
"marcelgwerder/laravel-api-handler": "dev-master"
14+
"marcelgwerder/laravel-api-handler": "~0.5.1"
1515
}
1616
```
1717
Then run `composer update`. Once composer finished add the service provider to the `providers` array in `app/config/app.php`:

src/Parser.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace Marcelgwerder\ApiHandler;
22

3+
use \Illuminate\Database\Eloquent\Relations\HasManyThrough;
4+
use \Illuminate\Database\Eloquent\Relations\HasOne;
35
use \Illuminate\Database\Eloquent\Relations\HasMany;
46
use \Illuminate\Database\Eloquent\Relations\BelongsTo;
57
use \Illuminate\Database\Eloquent\Relations\BelongsToMany;
@@ -407,47 +409,46 @@ protected function parseWith($withParam)
407409
$model = $relation->getModel();
408410
$relationType = $this->getRelationType($relation);
409411

410-
//Preserve backwards compatibility
411-
if(method_exists($relation , 'getQualifiedOtherKeyName') && method_exists($relation , 'getQualifiedForeignKey'))
412+
if($relationType === 'HasManyThrough')
412413
{
413-
$primaryKey = $relation->getOtherKey();
414-
$foreignKey = $relation->getQualifiedForeignKey();
415-
}
414+
$firstKey = $model->getKeyName();
415+
$secondKey = null;
416+
}
416417
else
417418
{
418419
$primaryKey = $model->getKeyName();
419420
$foreignKey = $relation->getForeignKey();
420421
}
421422

422423
//Switch keys according to the type of relationship
423-
if($relationType == 'HasMany')
424+
if($relationType === 'HasOne' || $relationType === 'HasMany' || $relationType === 'BelongsToMany')
424425
{
425426
$firstKey = $primaryKey;
426427
$secondKey = $foreignKey;
427428
}
428-
else if($relationType == 'BelongsTo')
429+
else if($relationType === 'BelongsTo')
429430
{
430431
$firstKey = $foreignKey;
431432
$secondKey = $primaryKey;
432433
}
433-
434+
434435
//Check if we're on level 1 (e.g. a and not a.b)
435-
if($previousHistoryPath == '')
436+
if($firstKey !== null && $previousHistoryPath == '')
436437
{
437438
if($fieldsCount > 0 && !in_array($primaryKey, $fields))
438439
{
439440
$fields[] = $firstKey;
440441
}
441442
}
442-
else
443+
else if($firstKey !== null)
443444
{
444445
if(count($withHistory[$previousHistoryPath]['fields']) > 0 && !in_array($firstKey, $withHistory[$previousHistoryPath]['fields']))
445446
{
446447
$withHistory[$previousHistoryPath]['fields'][] = $firstKey;
447448
}
448449
}
449450

450-
if(count($withHistory[$currentHistoryPath]['fields']) > 0 && !in_array($secondKey, $withHistory[$currentHistoryPath]['fields']))
451+
if($secondKey !== null && count($withHistory[$currentHistoryPath]['fields']) > 0 && !in_array($secondKey, $withHistory[$currentHistoryPath]['fields']))
451452
{
452453
$withHistory[$currentHistoryPath]['fields'][] = $secondKey;
453454
}
@@ -473,7 +474,7 @@ protected function parseWith($withParam)
473474
}, $withHistory[$withHistoryKey]['fields']);
474475

475476
if(count($fields) > 0 && is_array($fields))
476-
{
477+
{
477478
$query->select($fields);
478479
}
479480

@@ -722,6 +723,11 @@ protected function parseConfig($configParam)
722723
*/
723724
protected function getRelationType($relation)
724725
{
726+
if($relation instanceof HasOne)
727+
{
728+
return 'HasOne';
729+
}
730+
725731
if($relation instanceof HasMany)
726732
{
727733
return 'HasMany';
@@ -736,6 +742,11 @@ protected function getRelationType($relation)
736742
{
737743
return 'BelongsToMany';
738744
}
745+
746+
if($relation instanceof HasManyThrough)
747+
{
748+
return 'HasManyThrough';
749+
}
739750
}
740751

741752
/**

0 commit comments

Comments
 (0)