Skip to content

Commit 479f9a6

Browse files
Merge branch '7.x'
2 parents 5d2bd00 + b280edd commit 479f9a6

File tree

9 files changed

+91
-4
lines changed

9 files changed

+91
-4
lines changed

CHANGELOG-6.x.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Release Notes for 6.x
22

3-
## [Unreleased](https://github.com/laravel/framework/compare/v6.18.17...6.x)
3+
## [Unreleased](https://github.com/laravel/framework/compare/v6.18.18...6.x)
4+
5+
6+
## [v6.18.18 (2020-06-03)](https://github.com/laravel/framework/compare/v6.18.17...v6.18.18)
7+
8+
### Fixed
9+
- Fixed `Illuminate\Database\Eloquent\Relations\MorphToMany::getCurrentlyAttachedPivots()` ([110b129](https://github.com/laravel/framework/commit/110b129531df172f03bf163f561c71123fac6296))
410

511

612
## [v6.18.17 (2020-06-02)](https://github.com/laravel/framework/compare/v6.18.16...v6.18.17)

CHANGELOG-7.x.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
# Release Notes for 7.x
22

3-
## [Unreleased](https://github.com/laravel/framework/compare/v7.14.0...7.x)
3+
## [Unreleased](https://github.com/laravel/framework/compare/v7.14.1...7.x)
4+
5+
### Added
6+
- Added extendable relations for models ([#33025](https://github.com/laravel/framework/pull/33025))
7+
- Added `Illuminate\Foundation\Testing\Concerns\MakesHttpRequests::withToken()` ([#33075](https://github.com/laravel/framework/pull/33075), [79383a1](https://github.com/laravel/framework/commit/79383a129bf213177ff00ec1ba7c396da5d7749b))
8+
9+
10+
## [v7.14.1 (2020-06-03)](https://github.com/laravel/framework/compare/v7.14.0...v7.14.1)
411

512
### Added
613
- Added missing `symfony/mime` suggest ([#33067](https://github.com/laravel/framework/pull/33067))
714

15+
### Fixed
16+
- Fixed `Illuminate\Database\Eloquent\Relations\MorphToMany::getCurrentlyAttachedPivots()` ([110b129](https://github.com/laravel/framework/commit/110b129531df172f03bf163f561c71123fac6296))
17+
818

919
## [v7.14.0 (2020-06-02)](https://github.com/laravel/framework/compare/v7.13.0...v7.14.0)
1020

src/Illuminate/Database/Concerns/BuildsQueries.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function when($value, $callback, $default = null)
166166
* Pass the query to a given callback.
167167
*
168168
* @param callable $callback
169-
* @return \Illuminate\Database\Query\Builder
169+
* @return $this
170170
*/
171171
public function tap($callback)
172172
{

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ public function getRelationValue($key)
418418
// If the "attribute" exists as a method on the model, we will just assume
419419
// it is a relationship and will load and return results from the query
420420
// and hydrate the relationship's value on the "relationships" array.
421-
if (method_exists($this, $key)) {
421+
if (method_exists($this, $key) ||
422+
(static::$relationResolvers[get_class($this)][$key] ?? null)) {
422423
return $this->getRelationshipFromMethod($key);
423424
}
424425
}

src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Database\Eloquent\Concerns;
44

5+
use Closure;
56
use Illuminate\Collections\Arr;
67
use Illuminate\Database\Eloquent\Builder;
78
use Illuminate\Database\Eloquent\Collection;
@@ -44,6 +45,28 @@ trait HasRelationships
4445
'belongsToMany', 'morphToMany', 'morphedByMany',
4546
];
4647

48+
/**
49+
* The relation resolver callbacks.
50+
*
51+
* @var array
52+
*/
53+
protected static $relationResolvers = [];
54+
55+
/**
56+
* Define a dynamic relation resolver.
57+
*
58+
* @param string $name
59+
* @param \Closure $callback
60+
* @return void
61+
*/
62+
public static function resolveRelationUsing($name, Closure $callback)
63+
{
64+
static::$relationResolvers = array_replace_recursive(
65+
static::$relationResolvers,
66+
[static::class => [$name => $callback]]
67+
);
68+
}
69+
4770
/**
4871
* Define a one-to-one relationship.
4972
*

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,10 @@ public function __call($method, $parameters)
17191719
return $this->$method(...$parameters);
17201720
}
17211721

1722+
if ($resolver = (static::$relationResolvers[get_class($this)][$method] ?? null)) {
1723+
return $resolver($this);
1724+
}
1725+
17221726
return $this->forwardCallTo($this->newQuery(), $method, $parameters);
17231727
}
17241728

src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ public function withHeader(string $name, string $value)
8080
return $this;
8181
}
8282

83+
/**
84+
* Add an authorization token for the request.
85+
*
86+
* @param string $token
87+
* @param string $type
88+
* @return $this
89+
*/
90+
public function withToken(string $token, string $type = 'Bearer')
91+
{
92+
return $this->withHeader('Authorization', $type.' '.$token);
93+
}
94+
8395
/**
8496
* Flush all the configured headers.
8597
*

tests/Database/DatabaseEloquentRelationTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,20 @@ public function testMacroable()
267267
$result = $relation->foo();
268268
$this->assertSame('foo', $result);
269269
}
270+
271+
public function testRelationResolvers()
272+
{
273+
$model = new EloquentRelationResetModelStub;
274+
$builder = m::mock(Builder::class);
275+
$builder->shouldReceive('getModel')->andReturn($model);
276+
277+
EloquentRelationResetModelStub::resolveRelationUsing('customer', function ($model) use ($builder) {
278+
return new EloquentResolverRelationStub($builder, $model);
279+
});
280+
281+
$this->assertInstanceOf(EloquentResolverRelationStub::class, $model->customer());
282+
$this->assertSame(['key' => 'value'], $model->customer);
283+
}
270284
}
271285

272286
class EloquentRelationResetModelStub extends Model
@@ -329,3 +343,11 @@ class EloquentNoTouchingAnotherModelStub extends Model
329343
'id' => 2,
330344
];
331345
}
346+
347+
class EloquentResolverRelationStub extends EloquentRelationStub
348+
{
349+
public function getResults()
350+
{
351+
return ['key' => 'value'];
352+
}
353+
}

tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ public function testFromSetsHeaderAndSession()
1414
$this->assertSame('previous/url', $this->app['session']->previousUrl());
1515
}
1616

17+
public function testWithTokenSetsAuthorizationHeader()
18+
{
19+
$this->withToken('foobar');
20+
$this->assertSame('Bearer foobar', $this->defaultHeaders['Authorization']);
21+
22+
$this->withToken('foobar', 'Basic');
23+
$this->assertSame('Basic foobar', $this->defaultHeaders['Authorization']);
24+
}
25+
1726
public function testWithoutAndWithMiddleware()
1827
{
1928
$this->assertFalse($this->app->has('middleware.disable'));

0 commit comments

Comments
 (0)