Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit da4339c

Browse files
committed
-
1 parent c105da6 commit da4339c

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

‎src/Eloquent/HybridRelations.php‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ trait HybridRelations
2929
public function hasOne($related, $foreignKey = null, $localKey = null)
3030
{
3131
// Check if it is a relation with an original model.
32-
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
32+
if (! is_subclass_of($related, Model::class)) {
3333
return parent::hasOne($related, $foreignKey, $localKey);
3434
}
3535

@@ -55,7 +55,7 @@ public function hasOne($related, $foreignKey = null, $localKey = null)
5555
public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
5656
{
5757
// Check if it is a relation with an original model.
58-
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
58+
if (! is_subclass_of($related, Model::class)) {
5959
return parent::morphOne($related, $name, $type, $id, $localKey);
6060
}
6161

@@ -79,7 +79,7 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey =
7979
public function hasMany($related, $foreignKey = null, $localKey = null)
8080
{
8181
// Check if it is a relation with an original model.
82-
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
82+
if (! is_subclass_of($related, Model::class)) {
8383
return parent::hasMany($related, $foreignKey, $localKey);
8484
}
8585

@@ -105,7 +105,7 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
105105
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
106106
{
107107
// Check if it is a relation with an original model.
108-
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
108+
if (! is_subclass_of($related, Model::class)) {
109109
return parent::morphMany($related, $name, $type, $id, $localKey);
110110
}
111111

@@ -142,7 +142,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
142142
}
143143

144144
// Check if it is a relation with an original model.
145-
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
145+
if (! is_subclass_of($related, Model::class)) {
146146
return parent::belongsTo($related, $foreignKey, $otherKey, $relation);
147147
}
148148

@@ -211,13 +211,13 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
211211
/**
212212
* Define a many-to-many relationship.
213213
*
214-
* @param string $related
215-
* @param string $collection
216-
* @param string $foreignKey
217-
* @param string $otherKey
218-
* @param string $parentKey
219-
* @param string $relatedKey
220-
* @param string $relation
214+
* @param class-string<\Illuminate\Database\Eloquent\Model> $related
215+
* @param string|null $collection
216+
* @param string|null $foreignKey
217+
* @param string|null $otherKey
218+
* @param string|null $parentKey
219+
* @param string|null $relatedKey
220+
* @param string|null $relation
221221
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
222222
*/
223223
public function belongsToMany(
@@ -237,7 +237,7 @@ public function belongsToMany(
237237
}
238238

239239
// Check if it is a relation with an original model.
240-
if (! is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
240+
if (! is_subclass_of($related, Model::class)) {
241241
return parent::belongsToMany(
242242
$related,
243243
$collection,
@@ -249,13 +249,18 @@ public function belongsToMany(
249249
);
250250
}
251251

252+
252253
// First, we'll need to determine the foreign key and "other key" for the
253254
// relationship. Once we have determined the keys we'll make the query
254255
// instances as well as the relationship instances we need for this.
255256
$foreignKey = $foreignKey ?: $this->getForeignKey().'s';
256257

257258
$instance = new $related;
258259

260+
if ($otherKey === $relation) {
261+
throw new \LogicException(sprintf('In %s::%s(), the key cannot be identical to the relation name "%s". The default key is "%s".', static::class, $relation, $relation, $instance->getForeignKey().'s'));
262+
}
263+
259264
$otherKey = $otherKey ?: $instance->getForeignKey().'s';
260265

261266
// If no table name was provided, we can guess it by concatenating the two
@@ -301,7 +306,7 @@ protected function guessBelongsToManyRelation()
301306
*/
302307
public function newEloquentBuilder($query)
303308
{
304-
if (is_subclass_of($this, \Jenssegers\Mongodb\Eloquent\Model::class)) {
309+
if (is_subclass_of($this, Model::class)) {
305310
return new Builder($query);
306311
}
307312

‎tests/Models/Group.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ class Group extends Eloquent
1515

1616
public function users(): BelongsToMany
1717
{
18-
return $this->belongsToMany(User::class);
18+
return $this->belongsToMany(User::class, 'users', 'groups', 'userIds', '_id', '_id', 'users');
1919
}
2020
}

‎tests/Models/User.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ public function clients()
7979

8080
public function groups()
8181
{
82-
return $this->belongsToMany(Group::class);
82+
return $this->belongsToMany(Group::class, 'groups', 'users', 'groupIds', '_id', '_id', 'groups');
83+
}
84+
85+
public function otherGroups()
86+
{
87+
return $this->belongsToMany(Group::class, 'groups', 'users', 'otherGroups', '_id', '_id', 'groups');
8388
}
8489

8590
public function photos()

‎tests/RelationsTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ public function testBelongsToManyCustom(): void
344344
$group = Group::find($group->_id);
345345

346346
// Check for custom relation attributes
347-
$this->assertArrayHasKey('user_ids', $group->getAttributes());
348-
$this->assertArrayHasKey('group_ids', $user->getAttributes());
347+
$this->assertArrayHasKey('userIds', $group->getAttributes());
348+
$this->assertArrayHasKey('groupIds', $user->getAttributes());
349349

350350
// Assert they are attached
351351
$this->assertContains($group->_id, $user->groups->pluck('_id')->toArray());

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /