Skip to content

Commit 8d00250

Browse files
erikn69joelstein
andauthored
Add magic *_exists properties (#1712)
* Added magic `*_exists` properties. * fix return datatype * disabled by default --------- Co-authored-by: Joel Stein <[email protected]>
1 parent 877f5ed commit 8d00250

File tree

15 files changed

+51
-5
lines changed

15 files changed

+51
-5
lines changed

README.md

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

211211
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`.
212212

213-
#### Magic `*_count` properties
213+
#### Magic `*_count` and `*_exists` properties
214214

215-
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.
215+
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.
216216

217-
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`.
217+
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`.
218218

219219
#### Generics annotations
220220

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' => false,
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', false);
176179

177180
$this->write = $this->write_mixin ? true : $this->write;
178181
//If filename is default and Write is not specified, ask what to do
@@ -820,6 +823,15 @@ public function getPropertiesFromMethods($model)
820823
// What kind of comments should be added to the relation count here?
821824
);
822825
}
826+
if ($this->write_model_relation_exists_properties) {
827+
$this->setProperty(
828+
Str::snake($method) . '_exists',
829+
'bool|null',
830+
true,
831+
false
832+
// What kind of comments should be added to the relation count here?
833+
);
834+
}
823835
} elseif (
824836
$relationReturnType === 'morphTo' ||
825837
(

tests/Console/ModelsCommand/AbstractModelsCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,7 @@ protected function getEnvironmentSetUp($app)
5050

5151
// Don't override integer -> int for tests
5252
$config->set('ide-helper.type_overrides', []);
53+
54+
$config->set('ide-helper.write_model_relation_exists_properties', true);
5355
}
5456
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* @property-read string $not_comment
2222
* @property-read \Illuminate\Database\Eloquent\Collection<int, Simple> $relationHasMany HasMany relations.
2323
* @property-read int|null $relation_has_many_count
24+
* @property-read bool|null $relation_has_many_exists
2425
* @property-read Simple|null $relationHasOne Others relations.
2526
* @property-read Model|\Eloquent $relationMorphTo MorphTo relations.
2627
* @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
@@ -12,6 +12,7 @@
1212
* @property int $id
1313
* @property-read SimpleCollection<int, Simple> $relationHasMany
1414
* @property-read int|null $relation_has_many_count
15+
* @property-read bool|null $relation_has_many_exists
1516
* @method static SimpleCollection<int, static> all($columns = ['*'])
1617
* @method static SimpleCollection<int, static> get($columns = ['*'])
1718
* @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
@@ -12,6 +12,7 @@
1212
/**
1313
* @property-read \Illuminate\Database\Eloquent\Collection<int, Dynamic> $regularHasMany
1414
* @property-read int|null $regular_has_many_count
15+
* @property-read bool|null $regular_has_many_exists
1516
* @method static \Illuminate\Database\Eloquent\Builder<static>|Dynamic newModelQuery()
1617
* @method static \Illuminate\Database\Eloquent\Builder<static>|Dynamic newQuery()
1718
* @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
@@ -82,6 +82,7 @@
8282
* @property \Illuminate\Support\Carbon|null $updated_at
8383
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post> $posts
8484
* @property-read int|null $posts_count
85+
* @property-read bool|null $posts_exists
8586
* @method static \Illuminate\Database\Eloquent\Builder<static>|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post newModelQuery()
8687
* @method static \Illuminate\Database\Eloquent\Builder<static>|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWithForcedFqn\Models\Post newQuery()
8788
* @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
@@ -88,6 +88,7 @@
8888
* @property Carbon|null $updated_at
8989
* @property-read Collection<int, Post> $posts
9090
* @property-read int|null $posts_count
91+
* @property-read bool|null $posts_exists
9192
* @method static EloquentBuilder<static>|Post newModelQuery()
9293
* @method static EloquentBuilder<static>|Post newQuery()
9394
* @method static EloquentBuilder<static>|Post null(string $unusedParam)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
* @property int $id
1313
* @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $regularBelongsToMany
1414
* @property-read int|null $regular_belongs_to_many_count
15+
* @property-read bool|null $regular_belongs_to_many_exists
1516
* @property-read \Illuminate\Database\Eloquent\Collection|Simple[] $regularHasMany
1617
* @property-read int|null $regular_has_many_count
18+
* @property-read bool|null $regular_has_many_exists
1719
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newModelQuery()
1820
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newQuery()
1921
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple query()

0 commit comments

Comments
 (0)