diff --git a/README.md b/README.md index a6d97b1..d7f7bac 100644 --- a/README.md +++ b/README.md @@ -210,11 +210,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 6eb5b85..8ca0e08 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -85,14 +85,16 @@ return [ /* |-------------------------------------------------------------------------- - | 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' => false, /* |-------------------------------------------------------------------------- diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 8866a0f..4f22206 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 @@ class ModelsCommand extends Command $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', false); $this->write = $this->write_mixin ? true : $this->write; //If filename is default and Write is not specified, ask what to do @@ -820,6 +823,15 @@ class ModelsCommand extends Command // 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', + 'bool|null', + true, + false + // What kind of comments should be added to the relation count here? + ); + } } elseif ( $relationReturnType === 'morphTo' || ( diff --git a/tests/Console/ModelsCommand/AbstractModelsCommand.php b/tests/Console/ModelsCommand/AbstractModelsCommand.php index cece6be..25d3966 100644 --- a/tests/Console/ModelsCommand/AbstractModelsCommand.php +++ b/tests/Console/ModelsCommand/AbstractModelsCommand.php @@ -50,5 +50,7 @@ abstract class AbstractModelsCommand extends TestCase // Don't override integer -> int for tests $config->set('ide-helper.type_overrides', []); + + $config->set('ide-helper.write_model_relation_exists_properties', true); } } diff --git a/tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php index 40b2981..d8fc65e 100644 --- a/tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Comment/__snapshots__/Test__test__1.php @@ -21,6 +21,7 @@ use Illuminate\Database\Eloquent\Relations\MorphTo; * @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 fcea0a1..f9b38b8 100644 --- a/tests/Console/ModelsCommand/CustomCollection/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/CustomCollection/__snapshots__/Test__test__1.php @@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; * @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 d3ddf23..4e280d5 100644 --- a/tests/Console/ModelsCommand/DynamicRelations/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/DynamicRelations/__snapshots__/Test__test__1.php @@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Relations\HasOne; /** * @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 f067899..b95c9da 100644 --- a/tests/Console/ModelsCommand/GeneratePhpdocWithForcedFqn/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/GeneratePhpdocWithForcedFqn/__snapshots__/Test__test__1.php @@ -82,6 +82,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @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 b59895a..b307b80 100644 --- a/tests/Console/ModelsCommand/GeneratePhpdocWithFqn/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/GeneratePhpdocWithFqn/__snapshots__/Test__test__1.php @@ -88,6 +88,7 @@ use Illuminate\Support\Carbon; * @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 cc698aa..97bbdbc 100644 --- a/tests/Console/ModelsCommand/GenericsSyntaxDisabled/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/GenericsSyntaxDisabled/__snapshots__/Test__test__1.php @@ -12,8 +12,10 @@ use Illuminate\Database\Eloquent\Relations\HasMany; * @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 b7efb0e..b9efe69 100644 --- a/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Pivot/__snapshots__/Test__test__1.php @@ -12,16 +12,21 @@ use Illuminate\Database\Eloquent\Model; * @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 6be084b..2bf2822 100644 --- a/tests/Console/ModelsCommand/RelationCountProperties/Test.php +++ b/tests/Console/ModelsCommand/RelationCountProperties/Test.php @@ -14,6 +14,7 @@ class Test extends AbstractModelsCommand 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 124ac59..5c18063 100644 --- a/tests/Console/ModelsCommand/Relations/__snapshots__/Test__testRelationNotNullable__1.php +++ b/tests/Console/ModelsCommand/Relations/__snapshots__/Test__testRelationNotNullable__1.php @@ -133,26 +133,34 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany; * @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 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/Relations/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Relations/__snapshots__/Test__test__1.php index 645fc76..f836acf 100644 --- a/tests/Console/ModelsCommand/Relations/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Relations/__snapshots__/Test__test__1.php @@ -133,26 +133,34 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany; * @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 2bd5523..95341ed 100644 --- a/tests/Console/ModelsCommand/UnionTypes/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/UnionTypes/__snapshots__/Test__test__1.php @@ -12,6 +12,7 @@ use Illuminate\Database\Query\Builder; * @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()