Skip to content

Commit

Permalink
[5.4] Macroable relations (#17707)
Browse files Browse the repository at this point in the history
* Make relation macroable

* Add test for macroable relations
  • Loading branch information
thecrypticace authored and taylorotwell committed Feb 1, 2017
1 parent b39d7e2 commit 17a976a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Eloquent\Collection;

abstract class Relation
{
use Macroable {
__call as macroCall;
}

/**
* The Eloquent query builder instance.
*
Expand Down Expand Up @@ -328,6 +333,10 @@ protected static function buildMorphMapFromModels(array $models = null)
*/
public function __call($method, $parameters)
{
if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}

$result = call_user_func_array([$this->query, $method], $parameters);

if ($result === $this->query) {
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseEloquentRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ public function testSettingMorphMapWithNumericKeys()

Relation::morphMap([], false);
}

public function testMacroable()
{
Relation::macro('foo', function () {
return 'foo';
});

$model = new EloquentRelationResetModelStub();
$relation = new EloquentRelationStub($model->newQuery(), $model);

$result = $relation->foo();
$this->assertEquals('foo', $result);
}
}

class EloquentRelationResetModelStub extends Model
Expand Down

0 comments on commit 17a976a

Please sign in to comment.