Skip to content

Commit 4f1539b

Browse files
committed
Added magic *_exists properties.
1 parent 8d441ec commit 4f1539b

File tree

14 files changed

+49
-6
lines changed

14 files changed

+49
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ Eloquent allows calling `where<Attribute>` on your models, e.g. `Post::whereTitl
212212

213213
If for some reason it's undesired to have them generated (one for each column), you can disable this via config `write_model_magic_where` and setting it to `false`.
214214

215-
#### Magic `*_count` properties
215+
#### Magic `*_count` and `*_exists` properties
216216

217-
You may use the [`::withCount`](https://laravel.com/docs/master/eloquent-relationships#counting-related-models) method to count the number results from a relationship without actually loading them. Those results are then placed in attributes following the `<columname>_count` convention.
217+
You may use the [`::withCount`](https://laravel.com/docs/master/eloquent-relationships#counting-related-models) and [`::withExists`](https://laravel.com/docs/master/eloquent-relationships#other-aggregate-functions) methodsto count the number results from a relationship without actually loading them. Those results are then placed in attributes following the `<columname>_count` and `<columname>_exists` convention.
218218

219-
By default, these attributes are generated in the phpdoc. You can turn them off by setting the config `write_model_relation_count_properties` to `false`.
219+
By default, these attributes are generated in the phpdoc. You can turn them off by setting the config `write_model_relation_count_properties` and `write_model_relation_exists_properties` to `false`.
220220

221221
#### Generics annotations
222222

config/ide-helper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,16 @@
8585

8686
/*
8787
|--------------------------------------------------------------------------
88-
| Write model relation count properties
88+
| Write model relation count and exists properties
8989
|--------------------------------------------------------------------------
9090
|
91-
| Set to false to disable writing of relation count properties to model DocBlocks.
91+
| Set to false to disable writing of relation count and exists properties
92+
| to model DocBlocks.
9293
|
9394
*/
9495

9596
'write_model_relation_count_properties' => true,
97+
'write_model_relation_exists_properties' => true,
9698

9799
/*
98100
|--------------------------------------------------------------------------

src/Console/ModelsCommand.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class ModelsCommand extends Command
114114

115115
protected $write_model_magic_where;
116116
protected $write_model_relation_count_properties;
117+
protected $write_model_relation_exists_properties;
117118
protected $properties = [];
118119
protected $methods = [];
119120
protected $write = false;
@@ -173,6 +174,8 @@ public function handle()
173174
$this->write_model_external_builder_methods = $this->laravel['config']->get('ide-helper.write_model_external_builder_methods', true);
174175
$this->write_model_relation_count_properties =
175176
$this->laravel['config']->get('ide-helper.write_model_relation_count_properties', true);
177+
$this->write_model_relation_exists_properties =
178+
$this->laravel['config']->get('ide-helper.write_model_relation_exists_properties', true);
176179

177180
$this->write = $this->write_mixin ? true : $this->write;
178181
//If filename is default and Write is not specified, ask what to do
@@ -816,6 +819,15 @@ public function getPropertiesFromMethods($model)
816819
// What kind of comments should be added to the relation count here?
817820
);
818821
}
822+
if ($this->write_model_relation_exists_properties) {
823+
$this->setProperty(
824+
Str::snake($method) . '_exists',
825+
'int|null',
826+
true,
827+
false
828+
// What kind of comments should be added to the relation count here?
829+
);
830+
}
819831
} elseif (
820832
$relationReturnType === 'morphTo' ||
821833
(

tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @property-read string $not_comment
2424
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationHasMany HasMany relations.
2525
* @property-read int|null $relation_has_many_count
26+
* @property-read bool|null $relation_has_many_exists
2627
* @property-read Simple|null $relationHasOne Others relations.
2728
* @property-read Model|\Eloquent $relationMorphTo MorphTo relations.
2829
* @property-write mixed $first_name Set the user's first name.

tests/Console/ModelsCommand/CustomCollection/__snapshots__/Test__test__1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* @property int $id
1515
* @property-read SimpleCollection<int, Simple> $relationHasMany
1616
* @property-read int|null $relation_has_many_count
17+
* @property-read bool|null $relation_has_many_exists
1718
* @method static SimpleCollection<int, static> all($columns = ['*'])
1819
* @method static SimpleCollection<int, static> get($columns = ['*'])
1920
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newModelQuery()

tests/Console/ModelsCommand/DynamicRelations/__snapshots__/Test__test__1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*
1515
* @property-read \Illuminate\Database\Eloquent\Collection<int, Dynamic> $regularHasMany
1616
* @property-read int|null $regular_has_many_count
17+
* @property-read bool|null $regular_has_many_exists
1718
* @method static \Illuminate\Database\Eloquent\Builder<static>|Dynamic newModelQuery()
1819
* @method static \Illuminate\Database\Eloquent\Builder<static>|Dynamic newQuery()
1920
* @method static \Illuminate\Database\Eloquent\Builder<static>|Dynamic query()

tests/Console/ModelsCommand/GeneratePhpdocWithForcedFqn/__snapshots__/Test__test__1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
* @property \Illuminate\Support\Carbon|null $updated_at
8585
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post> $posts
8686
* @property-read int|null $posts_count
87+
* @property-read bool|null $posts_exists
8788
* @method static \Illuminate\Database\Eloquent\Builder<static>|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post newModelQuery()
8889
* @method static \Illuminate\Database\Eloquent\Builder<static>|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post newQuery()
8990
* @method static \Illuminate\Database\Eloquent\Builder<static>|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post null(string $unusedParam)

tests/Console/ModelsCommand/GeneratePhpdocWithFqn/__snapshots__/Test__test__1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
* @property Carbon|null $updated_at
9191
* @property-read Collection<int, Post> $posts
9292
* @property-read int|null $posts_count
93+
* @property-read bool|null $posts_exists
9394
* @method static EloquentBuilder<static>|Post newModelQuery()
9495
* @method static EloquentBuilder<static>|Post newQuery()
9596
* @method static EloquentBuilder<static>|Post null(string $unusedParam)

tests/Console/ModelsCommand/GenericsSyntaxDisabled/__snapshots__/Test__test__1.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* @property int $id
1515
* @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $regularBelongsToMany
1616
* @property-read int|null $regular_belongs_to_many_count
17+
* @property-read bool|null $regular_belongs_to_many_exists
1718
* @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $regularHasMany
18-
* @property-read int|null $regular_has_many_count
19+
* @property-read bool|null $regular_has_many_exists
1920
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newModelQuery()
2021
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newQuery()
2122
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple query()

tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@
1414
* @property-read DifferentCustomPivot|CustomPivot|null $pivot
1515
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationCustomPivotUsingSameAccessor
1616
* @property-read int|null $relation_custom_pivot_using_same_accessor_count
17+
* @property-read bool|null $relation_custom_pivot_using_same_accessor_exists
1718
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationCustomPivotUsingSameAccessorAndClass
1819
* @property-read int|null $relation_custom_pivot_using_same_accessor_and_class_count
20+
* @property-read bool|null $relation_custom_pivot_using_same_accessor_and_class_exists
1921
* @property-read CustomPivot|null $customAccessor
2022
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithCustomPivot
2123
* @property-read int|null $relation_with_custom_pivot_count
24+
* @property-read bool|null $relation_with_custom_pivot_exists
2225
* @property-read DifferentCustomPivot|null $differentCustomAccessor
2326
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithDifferentCustomPivot
2427
* @property-read int|null $relation_with_different_custom_pivot_count
28+
* @property-read bool|null $relation_with_different_custom_pivot_exists
2529
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $relationWithDifferentCustomPivotUsingSameAccessor
2630
* @property-read int|null $relation_with_different_custom_pivot_using_same_accessor_count
31+
* @property-read bool|null $relation_with_different_custom_pivot_using_same_accessor_exists
2732
* @method static \Illuminate\Database\Eloquent\Builder<static>|ModelWithPivot newModelQuery()
2833
* @method static \Illuminate\Database\Eloquent\Builder<static>|ModelWithPivot newQuery()
2934
* @method static \Illuminate\Database\Eloquent\Builder<static>|ModelWithPivot query()

tests/Console/ModelsCommand/RelationCountProperties/Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ protected function getEnvironmentSetUp($app)
1414
parent::getEnvironmentSetUp($app);
1515

1616
$app['config']->set('ide-helper.write_model_relation_count_properties', false);
17+
$app['config']->set('ide-helper.write_model_relation_exists_properties', false);
1718
}
1819

1920
public function test(): void

tests/Console/ModelsCommand/Relations/__snapshots__/Test__testRelationNotNullable__1.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,34 @@ public function nullableMixedWithForeignKeyConstraint(): BelongsTo
139139
* @property-read AnotherModel $relationBelongsToInAnotherNamespace
140140
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationBelongsToMany
141141
* @property-read int|null $relation_belongs_to_many_count
142+
* @property-read bool|null $relation_belongs_to_many_exists
142143
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationBelongsToManyWithSub
143144
* @property-read int|null $relation_belongs_to_many_with_sub_count
145+
* @property-read bool|null $relation_belongs_to_many_with_sub_exists
144146
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationBelongsToManyWithSubAnother
145147
* @property-read int|null $relation_belongs_to_many_with_sub_another_count
148+
* @property-read bool|null $relation_belongs_to_many_with_sub_another_exists
146149
* @property-read AnotherModel $relationBelongsToSameNameAsColumn
147150
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationHasMany
148151
* @property-read int|null $relation_has_many_count
152+
* @property-read bool|null $relation_has_many_exists
149153
* @property-read Simple|null $relationHasOne
150154
* @property-read Simple $relationHasOneWithDefault
151155
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationMorphMany
152156
* @property-read int|null $relation_morph_many_count
157+
* @property-read bool|null $relation_morph_many_exists
153158
* @property-read Simple|null $relationMorphOne
154159
* @property-read Model|\Eloquent $relationMorphTo
155160
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationMorphedByMany
156161
* @property-read int|null $relation_morphed_by_many_count
162+
* @property-read bool|null $relation_morphed_by_many_exists
157163
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationSampleRelationType
158164
* @property-read int|null $relation_sample_relation_type_count
165+
* @property-read boo|null $relation_sample_relation_type_exists
159166
* @property-read Model|\Eloquent $relationSampleToAnyMorphedRelationType
160167
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationSampleToAnyRelationType
161168
* @property-read int|null $relation_sample_to_any_relation_type_count
169+
* @property-read bool|null $relation_sample_to_any_relation_type_exists
162170
* @property-read Simple $relationSampleToBadlyNamedNotManyRelation
163171
* @property-read Simple $relationSampleToManyRelationType
164172
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newModelQuery()

tests/Console/ModelsCommand/Relations/__snapshots__/Test__test__1.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,34 @@ public function nullableMixedWithForeignKeyConstraint(): BelongsTo
139139
* @property-read AnotherModel|null $relationBelongsToInAnotherNamespace
140140
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationBelongsToMany
141141
* @property-read int|null $relation_belongs_to_many_count
142+
* @property-read bool|null $relation_belongs_to_many_exists
142143
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationBelongsToManyWithSub
143144
* @property-read int|null $relation_belongs_to_many_with_sub_count
145+
* @property-read bool|null $relation_belongs_to_many_with_sub_exists
144146
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationBelongsToManyWithSubAnother
145147
* @property-read int|null $relation_belongs_to_many_with_sub_another_count
148+
* @property-read bool|null $relation_belongs_to_many_with_sub_another_exists
146149
* @property-read AnotherModel|null $relationBelongsToSameNameAsColumn
147150
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationHasMany
148151
* @property-read int|null $relation_has_many_count
152+
* @property-read bool|null $relation_has_many_exists
149153
* @property-read Simple|null $relationHasOne
150154
* @property-read Simple $relationHasOneWithDefault
151155
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationMorphMany
152156
* @property-read int|null $relation_morph_many_count
157+
* @property-read bool|null $relation_morph_many_exists
153158
* @property-read Simple|null $relationMorphOne
154159
* @property-read Model|\Eloquent $relationMorphTo
155160
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationMorphedByMany
156161
* @property-read int|null $relation_morphed_by_many_count
162+
* @property-read bool|null $relation_morphed_by_many_exists
157163
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationSampleRelationType
158164
* @property-read int|null $relation_sample_relation_type_count
165+
* @property-read bool|null $relation_sample_relation_type_exists
159166
* @property-read Model|\Eloquent $relationSampleToAnyMorphedRelationType
160167
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationSampleToAnyRelationType
161168
* @property-read int|null $relation_sample_to_any_relation_type_count
169+
* @property-read bool|null $relation_sample_to_any_relation_type_exists
162170
* @property-read Simple $relationSampleToBadlyNamedNotManyRelation
163171
* @property-read Simple $relationSampleToManyRelationType
164172
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newModelQuery()

tests/Console/ModelsCommand/UnionTypes/__snapshots__/Test__test__1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* @property-read string|int|null $foo
1515
* @property-read \Illuminate\Database\Eloquent\Collection<int, UnionTypeModel> $withUnionTypeReturn
1616
* @property-read int|null $with_union_type_return_count
17+
* @property-read bool|null $with_union_type_return_exists
1718
* @method static \Illuminate\Database\Eloquent\Builder<static>|UnionTypeModel newModelQuery()
1819
* @method static \Illuminate\Database\Eloquent\Builder<static>|UnionTypeModel newQuery()
1920
* @method static \Illuminate\Database\Eloquent\Builder<static>|UnionTypeModel query()

0 commit comments

Comments
 (0)