Skip to content

Commit bc437a8

Browse files
authored
Merge pull request #18 from this-is-raj/master
Feat: added functionality for fetching results in belongs to many rel…
2 parents 73bb913 + 6811486 commit bc437a8

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/ApiController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Illuminate\Database\Eloquent\Collection;
1010
use Illuminate\Database\Eloquent\Model;
1111
use Illuminate\Database\Eloquent\Relations\BelongsTo;
12-
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
12+
use Froiden\RestAPI\ExtendedRelations\BelongsToMany;
1313
use Illuminate\Database\Eloquent\Relations\HasMany;
1414
use Illuminate\Database\Eloquent\Relations\HasOne;
1515
use Illuminate\Database\Eloquent\Relations\Relation;
@@ -424,7 +424,7 @@ protected function addIncludes()
424424

425425
$q->select($fields)
426426
->join(\DB::raw("(" . $outerQuery->toSql() . ") as `outer_query`"), function ($join) use($q) {
427-
$join->on("outer_query.id", "=", $q->getQualifiedRelatedPivotKeyName ());
427+
$join->on("outer_query." . $q->getRelatedKeyName(), "=", $q->getQualifiedRelatedPivotKeyName ());
428428
$join->on("outer_query.whatever", "=", $q->getQualifiedForeignPivotKeyName());
429429
})
430430
->setBindings(array_merge($q->getQuery()->getBindings(), $outerQuery->getBindings()))
@@ -870,4 +870,4 @@ protected function setQuery($query) {
870870
}
871871

872872
//endregion
873-
}
873+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Raj Kumar
5+
* Date: 2/12/19
6+
* Time: 5:08 PM
7+
*/
8+
9+
namespace Froiden\RestAPI\ExtendedRelations;
10+
11+
12+
use Illuminate\Database\Eloquent\Relations\BelongsToMany as LaravelBelongsToMany;
13+
14+
class BelongsToMany extends LaravelBelongsToMany
15+
{
16+
/**
17+
* @return string
18+
*/
19+
public function getRelatedKeyName() {
20+
return $this->relatedKey ?: 'id';
21+
}
22+
}

0 commit comments

Comments
 (0)