|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Integration\Database\EloquentWithCountTest; |
| 4 | + |
| 5 | +use Orchestra\Testbench\TestCase; |
| 6 | +use Illuminate\Support\Facades\Schema; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | + |
| 9 | +/** |
| 10 | + * @group integration |
| 11 | + */ |
| 12 | +class EloquentWithCountTest extends TestCase |
| 13 | +{ |
| 14 | + protected function getEnvironmentSetUp($app) |
| 15 | + { |
| 16 | + $app['config']->set('app.debug', 'true'); |
| 17 | + |
| 18 | + $app['config']->set('database.default', 'testbench'); |
| 19 | + |
| 20 | + $app['config']->set('database.connections.testbench', [ |
| 21 | + 'driver' => 'sqlite', |
| 22 | + 'database' => ':memory:', |
| 23 | + 'prefix' => '', |
| 24 | + ]); |
| 25 | + } |
| 26 | + |
| 27 | + public function setUp() |
| 28 | + { |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + Schema::create('one', function ($table) { |
| 32 | + $table->increments('id'); |
| 33 | + }); |
| 34 | + |
| 35 | + Schema::create('two', function ($table) { |
| 36 | + $table->increments('id'); |
| 37 | + $table->integer('one_id'); |
| 38 | + }); |
| 39 | + |
| 40 | + Schema::create('three', function ($table) { |
| 41 | + $table->increments('id'); |
| 42 | + $table->integer('two_id'); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @test |
| 48 | + */ |
| 49 | + public function it_basic() |
| 50 | + { |
| 51 | + $one = Model1::create(); |
| 52 | + $two = $one->twos()->Create(); |
| 53 | + $three = $two->threes()->Create(); |
| 54 | + |
| 55 | + $results = Model1::withCount([ |
| 56 | + 'twos' => function ($query) { |
| 57 | + $query->where('id', '>=', 1); |
| 58 | + }, |
| 59 | + ]); |
| 60 | + |
| 61 | + $this->assertEquals([ |
| 62 | + ['id' => 1, 'twos_count' => 1], |
| 63 | + ], $results->get()->toArray()); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +class Model1 extends Model |
| 68 | +{ |
| 69 | + public $table = 'one'; |
| 70 | + public $timestamps = false; |
| 71 | + protected $guarded = ['id']; |
| 72 | + |
| 73 | + public function twos() |
| 74 | + { |
| 75 | + return $this->hasMany(Model2::class, 'one_id'); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +class Model2 extends Model |
| 80 | +{ |
| 81 | + public $table = 'two'; |
| 82 | + public $timestamps = false; |
| 83 | + protected $guarded = ['id']; |
| 84 | + protected $withCount = ['threes']; |
| 85 | + |
| 86 | + public function threes() |
| 87 | + { |
| 88 | + return $this->hasMany(Model3::class, 'two_id'); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +class Model3 extends Model |
| 93 | +{ |
| 94 | + public $table = 'three'; |
| 95 | + public $timestamps = false; |
| 96 | + protected $guarded = ['id']; |
| 97 | + |
| 98 | + protected static function boot() |
| 99 | + { |
| 100 | + parent::boot(); |
| 101 | + |
| 102 | + static::addGlobalScope('app', function ($builder) { |
| 103 | + $builder->where('idz', '>', 0); |
| 104 | + }); |
| 105 | + } |
| 106 | +} |
0 commit comments