-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[10.x] Allow for converting a HasMany to HasOne && MorphMany to Morph…
…One (#46443) * allow for converting a HasMany to HasOne && MorphMany to MorphOne * fixes model names for EloquentHasManyTest * style fixes * formatting * rename method --------- Co-authored-by: Taylor Otwell <[email protected]>
- Loading branch information
1 parent
3397861
commit 38f022d
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
use Illuminate\Database\Eloquent\Relations\HasOne; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class EloquentHasManyTest extends DatabaseTestCase | ||
{ | ||
protected function defineDatabaseMigrationsAfterDatabaseRefreshed() | ||
{ | ||
Schema::create('eloquent_has_many_test_users', function ($table) { | ||
$table->id(); | ||
}); | ||
|
||
Schema::create('eloquent_has_many_test_logins', function ($table) { | ||
$table->id(); | ||
$table->foreignId('eloquent_has_many_test_user_id'); | ||
$table->timestamp('login_time'); | ||
}); | ||
} | ||
|
||
public function testCanGetHasOneFromHasManyRelationship() | ||
{ | ||
$user = EloquentHasManyTestUser::create(); | ||
|
||
$user->logins()->create(['login_time' => now()]); | ||
|
||
$this->assertInstanceOf(HasOne::class, $user->logins()->one()); | ||
} | ||
|
||
public function testHasOneRelationshipFromHasMany() | ||
{ | ||
$user = EloquentHasManyTestUser::create(); | ||
|
||
EloquentHasManyTestLogin::create([ | ||
'eloquent_has_many_test_user_id' => $user->id, | ||
'login_time' => '2020-09-29', | ||
]); | ||
$latestLogin = EloquentHasManyTestLogin::create([ | ||
'eloquent_has_many_test_user_id' => $user->id, | ||
'login_time' => '2023-03-14', | ||
]); | ||
$oldestLogin = EloquentHasManyTestLogin::create([ | ||
'eloquent_has_many_test_user_id' => $user->id, | ||
'login_time' => '2010-01-01', | ||
]); | ||
|
||
$this->assertEquals($oldestLogin->id, $user->oldestLogin->id); | ||
$this->assertEquals($latestLogin->id, $user->latestLogin->id); | ||
} | ||
} | ||
|
||
class EloquentHasManyTestUser extends Model | ||
{ | ||
protected $guarded = []; | ||
public $timestamps = false; | ||
|
||
public function logins(): HasMany | ||
{ | ||
return $this->hasMany(EloquentHasManyTestLogin::class); | ||
} | ||
|
||
public function latestLogin(): HasOne | ||
{ | ||
return $this->logins()->one()->latestOfMany('login_time'); | ||
} | ||
|
||
public function oldestLogin(): HasOne | ||
{ | ||
return $this->logins()->one()->oldestOfMany('login_time'); | ||
} | ||
} | ||
|
||
class EloquentHasManyTestLogin extends Model | ||
{ | ||
protected $guarded = []; | ||
public $timestamps = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters