diff --git a/README.md b/README.md index fc6efafe5..e4095d895 100644 --- a/README.md +++ b/README.md @@ -212,11 +212,11 @@ Eloquent allows calling `where` on your models, e.g. `Post::whereTitl 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`. -#### Magic `*_count` properties +#### Magic `*_count` and `*_exists` properties -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 `_count` convention. +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 `_count` and `_exists` convention. -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`. +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`. #### Generics annotations diff --git a/config/ide-helper.php b/config/ide-helper.php index 1500783aa..41df13ebd 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -85,14 +85,16 @@ /* |-------------------------------------------------------------------------- - | Write model relation count properties + | Write model relation count and exists properties |-------------------------------------------------------------------------- | - | Set to false to disable writing of relation count properties to model DocBlocks. + | Set to false to disable writing of relation count and exists properties + | to model DocBlocks. | */ 'write_model_relation_count_properties' => true, + 'write_model_relation_exists_properties' => true, /* |-------------------------------------------------------------------------- diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 395cf9d79..3aec99778 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -114,6 +114,7 @@ class ModelsCommand extends Command protected $write_model_magic_where; protected $write_model_relation_count_properties; + protected $write_model_relation_exists_properties; protected $properties = []; protected $methods = []; protected $write = false; @@ -173,6 +174,8 @@ public function handle() $this->write_model_external_builder_methods = $this->laravel['config']->get('ide-helper.write_model_external_builder_methods', true); $this->write_model_relation_count_properties = $this->laravel['config']->get('ide-helper.write_model_relation_count_properties', true); + $this->write_model_relation_exists_properties = + $this->laravel['config']->get('ide-helper.write_model_relation_exists_properties', true); $this->write = $this->write_mixin ? true : $this->write; //If filename is default and Write is not specified, ask what to do @@ -816,6 +819,15 @@ public function getPropertiesFromMethods($model) // What kind of comments should be added to the relation count here? ); } + if ($this->write_model_relation_exists_properties) { + $this->setProperty( + Str::snake($method) . '_exists', + 'int|null', + true, + false + // What kind of comments should be added to the relation count here? + ); + } } elseif ( $relationReturnType === 'morphTo' || ( diff --git a/tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php index 71806e75e..6a41aa610 100644 --- a/tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php @@ -23,6 +23,7 @@ * @property-read string $not_comment * @property-read \Illuminate\Database\Eloquent\Collection $relationHasMany HasMany relations. * @property-read int|null $relation_has_many_count + * @property-read bool|null $relation_has_many_exists * @property-read Simple|null $relationHasOne Others relations. * @property-read Model|\Eloquent $relationMorphTo MorphTo relations. * @property-write mixed $first_name Set the user's first name. diff --git a/tests/Console/ModelsCommand/CustomCollection/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/CustomCollection/__snapshots__/Test__test__1.php index b2a1211f1..230a65be4 100644 --- a/tests/Console/ModelsCommand/CustomCollection/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/CustomCollection/__snapshots__/Test__test__1.php @@ -14,6 +14,7 @@ * @property int $id * @property-read SimpleCollection $relationHasMany * @property-read int|null $relation_has_many_count + * @property-read bool|null $relation_has_many_exists * @method static SimpleCollection all($columns = ['*']) * @method static SimpleCollection get($columns = ['*']) * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery() diff --git a/tests/Console/ModelsCommand/DynamicRelations/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/DynamicRelations/__snapshots__/Test__test__1.php index 5e8bc21ea..64eb2d048 100644 --- a/tests/Console/ModelsCommand/DynamicRelations/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/DynamicRelations/__snapshots__/Test__test__1.php @@ -14,6 +14,7 @@ * * @property-read \Illuminate\Database\Eloquent\Collection $regularHasMany * @property-read int|null $regular_has_many_count + * @property-read bool|null $regular_has_many_exists * @method static \Illuminate\Database\Eloquent\Builder|Dynamic newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Dynamic newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Dynamic query() diff --git a/tests/Console/ModelsCommand/GeneratePhpdocWithForcedFqn/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/GeneratePhpdocWithForcedFqn/__snapshots__/Test__test__1.php index 09e1402a2..5a0da66e0 100644 --- a/tests/Console/ModelsCommand/GeneratePhpdocWithForcedFqn/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/GeneratePhpdocWithForcedFqn/__snapshots__/Test__test__1.php @@ -84,6 +84,7 @@ * @property \Illuminate\Support\Carbon|null $updated_at * @property-read \Illuminate\Database\Eloquent\Collection $posts * @property-read int|null $posts_count + * @property-read bool|null $posts_exists * @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post newQuery() * @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post null(string $unusedParam) diff --git a/tests/Console/ModelsCommand/GeneratePhpdocWithFqn/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/GeneratePhpdocWithFqn/__snapshots__/Test__test__1.php index ea78b8f33..38ee19ad3 100644 --- a/tests/Console/ModelsCommand/GeneratePhpdocWithFqn/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/GeneratePhpdocWithFqn/__snapshots__/Test__test__1.php @@ -90,6 +90,7 @@ * @property Carbon|null $updated_at * @property-read Collection $posts * @property-read int|null $posts_count + * @property-read bool|null $posts_exists * @method static EloquentBuilder|Post newModelQuery() * @method static EloquentBuilder|Post newQuery() * @method static EloquentBuilder|Post null(string $unusedParam) diff --git a/tests/Console/ModelsCommand/GenericsSyntaxDisabled/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/GenericsSyntaxDisabled/__snapshots__/Test__test__1.php index fd80086c3..5c1507d4d 100644 --- a/tests/Console/ModelsCommand/GenericsSyntaxDisabled/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/GenericsSyntaxDisabled/__snapshots__/Test__test__1.php @@ -14,8 +14,9 @@ * @property int $id * @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $regularBelongsToMany * @property-read int|null $regular_belongs_to_many_count + * @property-read bool|null $regular_belongs_to_many_exists * @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $regularHasMany - * @property-read int|null $regular_has_many_count + * @property-read bool|null $regular_has_many_exists * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple query() diff --git a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php index b1dd87653..7e9ad6ea8 100644 --- a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php @@ -14,16 +14,21 @@ * @property-read DifferentCustomPivot|CustomPivot|null $pivot * @property-read \Illuminate\Database\Eloquent\Collection $relationCustomPivotUsingSameAccessor * @property-read int|null $relation_custom_pivot_using_same_accessor_count + * @property-read bool|null $relation_custom_pivot_using_same_accessor_exists * @property-read \Illuminate\Database\Eloquent\Collection $relationCustomPivotUsingSameAccessorAndClass * @property-read int|null $relation_custom_pivot_using_same_accessor_and_class_count + * @property-read bool|null $relation_custom_pivot_using_same_accessor_and_class_exists * @property-read CustomPivot|null $customAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithCustomPivot * @property-read int|null $relation_with_custom_pivot_count + * @property-read bool|null $relation_with_custom_pivot_exists * @property-read DifferentCustomPivot|null $differentCustomAccessor * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivot * @property-read int|null $relation_with_different_custom_pivot_count + * @property-read bool|null $relation_with_different_custom_pivot_exists * @property-read \Illuminate\Database\Eloquent\Collection $relationWithDifferentCustomPivotUsingSameAccessor * @property-read int|null $relation_with_different_custom_pivot_using_same_accessor_count + * @property-read bool|null $relation_with_different_custom_pivot_using_same_accessor_exists * @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot newQuery() * @method static \Illuminate\Database\Eloquent\Builder|ModelWithPivot query() diff --git a/tests/Console/ModelsCommand/RelationCountProperties/Test.php b/tests/Console/ModelsCommand/RelationCountProperties/Test.php index 6be084bdd..2bf282208 100644 --- a/tests/Console/ModelsCommand/RelationCountProperties/Test.php +++ b/tests/Console/ModelsCommand/RelationCountProperties/Test.php @@ -14,6 +14,7 @@ protected function getEnvironmentSetUp($app) parent::getEnvironmentSetUp($app); $app['config']->set('ide-helper.write_model_relation_count_properties', false); + $app['config']->set('ide-helper.write_model_relation_exists_properties', false); } public function test(): void diff --git a/tests/Console/ModelsCommand/Relations/__snapshots__/Test__testRelationNotNullable__1.php b/tests/Console/ModelsCommand/Relations/__snapshots__/Test__testRelationNotNullable__1.php index 2c2b66cd1..0840b2cb5 100644 --- a/tests/Console/ModelsCommand/Relations/__snapshots__/Test__testRelationNotNullable__1.php +++ b/tests/Console/ModelsCommand/Relations/__snapshots__/Test__testRelationNotNullable__1.php @@ -139,26 +139,34 @@ public function nullableMixedWithForeignKeyConstraint(): BelongsTo * @property-read AnotherModel $relationBelongsToInAnotherNamespace * @property-read \Illuminate\Database\Eloquent\Collection $relationBelongsToMany * @property-read int|null $relation_belongs_to_many_count + * @property-read bool|null $relation_belongs_to_many_exists * @property-read \Illuminate\Database\Eloquent\Collection $relationBelongsToManyWithSub * @property-read int|null $relation_belongs_to_many_with_sub_count + * @property-read bool|null $relation_belongs_to_many_with_sub_exists * @property-read \Illuminate\Database\Eloquent\Collection $relationBelongsToManyWithSubAnother * @property-read int|null $relation_belongs_to_many_with_sub_another_count + * @property-read bool|null $relation_belongs_to_many_with_sub_another_exists * @property-read AnotherModel $relationBelongsToSameNameAsColumn * @property-read \Illuminate\Database\Eloquent\Collection $relationHasMany * @property-read int|null $relation_has_many_count + * @property-read bool|null $relation_has_many_exists * @property-read Simple|null $relationHasOne * @property-read Simple $relationHasOneWithDefault * @property-read \Illuminate\Database\Eloquent\Collection $relationMorphMany * @property-read int|null $relation_morph_many_count + * @property-read bool|null $relation_morph_many_exists * @property-read Simple|null $relationMorphOne * @property-read Model|\Eloquent $relationMorphTo * @property-read \Illuminate\Database\Eloquent\Collection $relationMorphedByMany * @property-read int|null $relation_morphed_by_many_count + * @property-read bool|null $relation_morphed_by_many_exists * @property-read \Illuminate\Database\Eloquent\Collection $relationSampleRelationType * @property-read int|null $relation_sample_relation_type_count + * @property-read boo|null $relation_sample_relation_type_exists * @property-read Model|\Eloquent $relationSampleToAnyMorphedRelationType * @property-read \Illuminate\Database\Eloquent\Collection $relationSampleToAnyRelationType * @property-read int|null $relation_sample_to_any_relation_type_count + * @property-read bool|null $relation_sample_to_any_relation_type_exists * @property-read Simple $relationSampleToBadlyNamedNotManyRelation * @property-read Simple $relationSampleToManyRelationType * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery() diff --git a/tests/Console/ModelsCommand/Relations/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Relations/__snapshots__/Test__test__1.php index 919c1d289..c279437c1 100644 --- a/tests/Console/ModelsCommand/Relations/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Relations/__snapshots__/Test__test__1.php @@ -139,26 +139,34 @@ public function nullableMixedWithForeignKeyConstraint(): BelongsTo * @property-read AnotherModel|null $relationBelongsToInAnotherNamespace * @property-read \Illuminate\Database\Eloquent\Collection $relationBelongsToMany * @property-read int|null $relation_belongs_to_many_count + * @property-read bool|null $relation_belongs_to_many_exists * @property-read \Illuminate\Database\Eloquent\Collection $relationBelongsToManyWithSub * @property-read int|null $relation_belongs_to_many_with_sub_count + * @property-read bool|null $relation_belongs_to_many_with_sub_exists * @property-read \Illuminate\Database\Eloquent\Collection $relationBelongsToManyWithSubAnother * @property-read int|null $relation_belongs_to_many_with_sub_another_count + * @property-read bool|null $relation_belongs_to_many_with_sub_another_exists * @property-read AnotherModel|null $relationBelongsToSameNameAsColumn * @property-read \Illuminate\Database\Eloquent\Collection $relationHasMany * @property-read int|null $relation_has_many_count + * @property-read bool|null $relation_has_many_exists * @property-read Simple|null $relationHasOne * @property-read Simple $relationHasOneWithDefault * @property-read \Illuminate\Database\Eloquent\Collection $relationMorphMany * @property-read int|null $relation_morph_many_count + * @property-read bool|null $relation_morph_many_exists * @property-read Simple|null $relationMorphOne * @property-read Model|\Eloquent $relationMorphTo * @property-read \Illuminate\Database\Eloquent\Collection $relationMorphedByMany * @property-read int|null $relation_morphed_by_many_count + * @property-read bool|null $relation_morphed_by_many_exists * @property-read \Illuminate\Database\Eloquent\Collection $relationSampleRelationType * @property-read int|null $relation_sample_relation_type_count + * @property-read bool|null $relation_sample_relation_type_exists * @property-read Model|\Eloquent $relationSampleToAnyMorphedRelationType * @property-read \Illuminate\Database\Eloquent\Collection $relationSampleToAnyRelationType * @property-read int|null $relation_sample_to_any_relation_type_count + * @property-read bool|null $relation_sample_to_any_relation_type_exists * @property-read Simple $relationSampleToBadlyNamedNotManyRelation * @property-read Simple $relationSampleToManyRelationType * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery() diff --git a/tests/Console/ModelsCommand/UnionTypes/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/UnionTypes/__snapshots__/Test__test__1.php index 720c52883..325b05a8a 100644 --- a/tests/Console/ModelsCommand/UnionTypes/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/UnionTypes/__snapshots__/Test__test__1.php @@ -14,6 +14,7 @@ * @property-read string|int|null $foo * @property-read \Illuminate\Database\Eloquent\Collection $withUnionTypeReturn * @property-read int|null $with_union_type_return_count + * @property-read bool|null $with_union_type_return_exists * @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel newQuery() * @method static \Illuminate\Database\Eloquent\Builder|UnionTypeModel query()