Add magic *_exists properties (#1712)

* Added magic `*_exists` properties.

* fix return datatype

* disabled by default

---------

Co-authored-by: Joel Stein <[email protected]>
This commit is contained in:
erikn69
2025-07-17 22:11:57 +02:00
committed by GitHub
co-authored by Joel Stein
parent 877f5ed0a7
commit 8d00250cba
15 changed files with 51 additions and 5 deletions
+3 -3
View File
@@ -210,11 +210,11 @@ Eloquent allows calling `where<Attribute>` 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 `<columname>_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 `<columname>_count` and `<columname>_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
+4 -2
View File
@@ -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,
/*
|--------------------------------------------------------------------------
+12
View File
@@ -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' ||
(
@@ -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);
}
}
@@ -21,6 +21,7 @@ use Illuminate\Database\Eloquent\Relations\MorphTo;
* @property-read string $not_comment
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $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.
@@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
* @property int $id
* @property-read SimpleCollection<int, Simple> $relationHasMany
* @property-read int|null $relation_has_many_count
* @property-read bool|null $relation_has_many_exists
* @method static SimpleCollection<int, static> all($columns = ['*'])
* @method static SimpleCollection<int, static> get($columns = ['*'])
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newModelQuery()
@@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Relations\HasOne;
/**
* @property-read \Illuminate\Database\Eloquent\Collection<int, Dynamic> $regularHasMany
* @property-read int|null $regular_has_many_count
* @property-read bool|null $regular_has_many_exists
* @method static \Illuminate\Database\Eloquent\Builder<static>|Dynamic newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Dynamic newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Dynamic query()
@@ -82,6 +82,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post> $posts
* @property-read int|null $posts_count
* @property-read bool|null $posts_exists
* @method static \Illuminate\Database\Eloquent\Builder<static>|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post null(string $unusedParam)
@@ -88,6 +88,7 @@ use Illuminate\Support\Carbon;
* @property Carbon|null $updated_at
* @property-read Collection<int, Post> $posts
* @property-read int|null $posts_count
* @property-read bool|null $posts_exists
* @method static EloquentBuilder<static>|Post newModelQuery()
* @method static EloquentBuilder<static>|Post newQuery()
* @method static EloquentBuilder<static>|Post null(string $unusedParam)
@@ -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<static>|Simple newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple query()
@@ -12,16 +12,21 @@ use Illuminate\Database\Eloquent\Model;
* @property-read DifferentCustomPivot|CustomPivot|null $pivot
* @property-read \Illuminate\Database\Eloquent\Collection<int, ModelWithPivot> $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<int, ModelWithPivot> $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<int, ModelWithPivot> $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<int, ModelWithPivot> $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<int, ModelWithPivot> $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<static>|ModelWithPivot newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|ModelWithPivot newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|ModelWithPivot query()
@@ -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
@@ -133,26 +133,34 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany;
* @property-read AnotherModel $relationBelongsToInAnotherNamespace
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $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<int, Simple> $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<int, Simple> $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<int, Simple> $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<int, Simple> $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<int, Simple> $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<int, Simple> $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<int, Simple> $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<static>|Simple newModelQuery()
@@ -133,26 +133,34 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany;
* @property-read AnotherModel|null $relationBelongsToInAnotherNamespace
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $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<int, Simple> $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<int, Simple> $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<int, Simple> $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<int, Simple> $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<int, Simple> $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<int, Simple> $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<int, Simple> $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<static>|Simple newModelQuery()
@@ -12,6 +12,7 @@ use Illuminate\Database\Query\Builder;
* @property-read string|int|null $foo
* @property-read \Illuminate\Database\Eloquent\Collection<int, UnionTypeModel> $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<static>|UnionTypeModel newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|UnionTypeModel newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|UnionTypeModel query()