EloquentWithCountTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\EloquentWithCountTest;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\Schema;
  6. use Illuminate\Tests\Integration\Database\DatabaseTestCase;
  7. class EloquentWithCountTest extends DatabaseTestCase
  8. {
  9. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  10. {
  11. Schema::create('one', function (Blueprint $table) {
  12. $table->increments('id');
  13. });
  14. Schema::create('two', function (Blueprint $table) {
  15. $table->increments('id');
  16. $table->integer('one_id');
  17. });
  18. Schema::create('three', function (Blueprint $table) {
  19. $table->increments('id');
  20. $table->integer('two_id');
  21. });
  22. Schema::create('four', function (Blueprint $table) {
  23. $table->increments('id');
  24. $table->integer('one_id');
  25. });
  26. }
  27. public function testItBasic()
  28. {
  29. $one = Model1::create();
  30. $two = $one->twos()->Create();
  31. $two->threes()->Create();
  32. $results = Model1::withCount([
  33. 'twos' => function ($query) {
  34. $query->where('id', '>=', 1);
  35. },
  36. ]);
  37. $this->assertEquals([
  38. ['id' => 1, 'twos_count' => 1],
  39. ], $results->get()->toArray());
  40. }
  41. public function testGlobalScopes()
  42. {
  43. $one = Model1::create();
  44. $one->fours()->create();
  45. $result = Model1::withCount('fours')->first();
  46. $this->assertEquals(0, $result->fours_count);
  47. $result = Model1::withCount('allFours')->first();
  48. $this->assertEquals(1, $result->all_fours_count);
  49. }
  50. public function testSortingScopes()
  51. {
  52. $one = Model1::create();
  53. $one->twos()->create();
  54. $query = Model1::withCount('twos')->getQuery();
  55. $this->assertNull($query->orders);
  56. $this->assertSame([], $query->getRawBindings()['order']);
  57. }
  58. }
  59. class Model1 extends Model
  60. {
  61. public $table = 'one';
  62. public $timestamps = false;
  63. protected $guarded = [];
  64. public function twos()
  65. {
  66. return $this->hasMany(Model2::class, 'one_id');
  67. }
  68. public function fours()
  69. {
  70. return $this->hasMany(Model4::class, 'one_id');
  71. }
  72. public function allFours()
  73. {
  74. return $this->fours()->withoutGlobalScopes();
  75. }
  76. }
  77. class Model2 extends Model
  78. {
  79. public $table = 'two';
  80. public $timestamps = false;
  81. protected $guarded = [];
  82. protected $withCount = ['threes'];
  83. protected static function boot()
  84. {
  85. parent::boot();
  86. static::addGlobalScope('app', function ($builder) {
  87. $builder->latest();
  88. });
  89. }
  90. public function threes()
  91. {
  92. return $this->hasMany(Model3::class, 'two_id');
  93. }
  94. }
  95. class Model3 extends Model
  96. {
  97. public $table = 'three';
  98. public $timestamps = false;
  99. protected $guarded = [];
  100. protected static function boot()
  101. {
  102. parent::boot();
  103. static::addGlobalScope('app', function ($builder) {
  104. $builder->where('id', '>', 0);
  105. });
  106. }
  107. }
  108. class Model4 extends Model
  109. {
  110. public $table = 'four';
  111. public $timestamps = false;
  112. protected $guarded = [];
  113. protected static function boot()
  114. {
  115. parent::boot();
  116. static::addGlobalScope('app', function ($builder) {
  117. $builder->where('id', '>', 1);
  118. });
  119. }
  120. }