EloquentWhereHasTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\EloquentWhereHasTest;
  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 EloquentWhereHasTest extends DatabaseTestCase
  8. {
  9. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  10. {
  11. Schema::create('users', function (Blueprint $table) {
  12. $table->increments('id');
  13. });
  14. Schema::create('posts', function (Blueprint $table) {
  15. $table->increments('id');
  16. $table->unsignedInteger('user_id');
  17. $table->boolean('public');
  18. });
  19. Schema::create('texts', function (Blueprint $table) {
  20. $table->increments('id');
  21. $table->unsignedInteger('post_id');
  22. $table->text('content');
  23. });
  24. Schema::create('comments', function (Blueprint $table) {
  25. $table->increments('id');
  26. $table->string('commentable_type');
  27. $table->integer('commentable_id');
  28. });
  29. $user = User::create();
  30. $post = tap((new Post(['public' => true]))->user()->associate($user))->save();
  31. (new Comment)->commentable()->associate($post)->save();
  32. (new Text(['content' => 'test']))->post()->associate($post)->save();
  33. $user = User::create();
  34. $post = tap((new Post(['public' => false]))->user()->associate($user))->save();
  35. (new Comment)->commentable()->associate($post)->save();
  36. (new Text(['content' => 'test2']))->post()->associate($post)->save();
  37. }
  38. public function testWhereRelation()
  39. {
  40. $users = User::whereRelation('posts', 'public', true)->get();
  41. $this->assertEquals([1], $users->pluck('id')->all());
  42. }
  43. public function testOrWhereRelation()
  44. {
  45. $users = User::whereRelation('posts', 'public', true)->orWhereRelation('posts', 'public', false)->get();
  46. $this->assertEquals([1, 2], $users->pluck('id')->all());
  47. }
  48. public function testNestedWhereRelation()
  49. {
  50. $texts = User::whereRelation('posts.texts', 'content', 'test')->get();
  51. $this->assertEquals([1], $texts->pluck('id')->all());
  52. }
  53. public function testNestedOrWhereRelation()
  54. {
  55. $texts = User::whereRelation('posts.texts', 'content', 'test')->orWhereRelation('posts.texts', 'content', 'test2')->get();
  56. $this->assertEquals([1, 2], $texts->pluck('id')->all());
  57. }
  58. public function testWhereMorphRelation()
  59. {
  60. $comments = Comment::whereMorphRelation('commentable', '*', 'public', true)->get();
  61. $this->assertEquals([1], $comments->pluck('id')->all());
  62. }
  63. public function testOrWhereMorphRelation()
  64. {
  65. $comments = Comment::whereMorphRelation('commentable', '*', 'public', true)
  66. ->orWhereMorphRelation('commentable', '*', 'public', false)
  67. ->get();
  68. $this->assertEquals([1, 2], $comments->pluck('id')->all());
  69. }
  70. public function testWithCount()
  71. {
  72. $users = User::whereHas('posts', function ($query) {
  73. $query->where('public', true);
  74. })->get();
  75. $this->assertEquals([1], $users->pluck('id')->all());
  76. }
  77. }
  78. class Comment extends Model
  79. {
  80. public $timestamps = false;
  81. public function commentable()
  82. {
  83. return $this->morphTo();
  84. }
  85. }
  86. class Post extends Model
  87. {
  88. public $timestamps = false;
  89. protected $guarded = [];
  90. protected $withCount = ['comments'];
  91. public function comments()
  92. {
  93. return $this->morphMany(Comment::class, 'commentable');
  94. }
  95. public function texts()
  96. {
  97. return $this->hasMany(Text::class);
  98. }
  99. public function user()
  100. {
  101. return $this->belongsTo(User::class);
  102. }
  103. }
  104. class Text extends Model
  105. {
  106. public $timestamps = false;
  107. protected $guarded = [];
  108. public function post()
  109. {
  110. return $this->belongsTo(Post::class);
  111. }
  112. }
  113. class User extends Model
  114. {
  115. public $timestamps = false;
  116. public function posts()
  117. {
  118. return $this->hasMany(Post::class);
  119. }
  120. }