File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed
src/Illuminate/Database/Eloquent/Relations
tests/Integration/Database Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Illuminate \Database \Eloquent \Relations ;
4
4
5
+ use Illuminate \Database \Eloquent \Builder ;
5
6
use Illuminate \Database \Eloquent \Collection ;
6
7
use Illuminate \Database \Eloquent \Relations \Concerns \InteractsWithDictionary ;
7
8
@@ -24,7 +25,7 @@ class HasManyThrough extends HasOneOrManyThrough
24
25
public function one ()
25
26
{
26
27
return HasOneThrough::noConstraints (fn () => new HasOneThrough (
27
- $ this ->getQuery (),
28
+ tap ( $ this ->getQuery (), fn ( Builder $ query ) => $ query -> getQuery ()-> joins = [] ),
28
29
$ this ->farParent ,
29
30
$ this ->throughParent ,
30
31
$ this ->getFirstKeyName (),
Original file line number Diff line number Diff line change 3
3
namespace Illuminate \Tests \Integration \Database \EloquentHasManyThroughTest ;
4
4
5
5
use Illuminate \Database \Eloquent \Model ;
6
+ use Illuminate \Database \Eloquent \Relations \HasOneThrough ;
6
7
use Illuminate \Database \Eloquent \SoftDeletes ;
7
8
use Illuminate \Database \Schema \Blueprint ;
8
9
use Illuminate \Support \Facades \DB ;
@@ -381,6 +382,18 @@ public function testCanReplicateModelLoadedThroughHasManyThrough()
381
382
$ newArticle ->title .= ' v2 ' ;
382
383
$ newArticle ->save ();
383
384
}
385
+
386
+ public function testOne (): void
387
+ {
388
+ $ team = Team::create ();
389
+
390
+ $ user = User::create (['team_id ' => $ team ->id , 'name ' => Str::random ()]);
391
+
392
+ Article::create (['user_id ' => $ user ->id , 'title ' => Str::random (), 'created_at ' => now ()->subDay ()]);
393
+ $ latestArticle = Article::create (['user_id ' => $ user ->id , 'title ' => Str::random (), 'created_at ' => now ()]);
394
+
395
+ $ this ->assertEquals ($ latestArticle ->id , $ team ->latestArticle ->id );
396
+ }
384
397
}
385
398
386
399
class User extends Model
@@ -474,6 +487,11 @@ public function articles()
474
487
{
475
488
return $ this ->hasManyThrough (Article::class, User::class);
476
489
}
490
+
491
+ public function latestArticle (): HasOneThrough
492
+ {
493
+ return $ this ->articles ()->one ()->latest ();
494
+ }
477
495
}
478
496
479
497
class Category extends Model
You can’t perform that action at this time.
0 commit comments