Skip to content

Commit e5aafea

Browse files
committed
Ignore column properties via config and unset properties in hooks
1 parent 999167d commit e5aafea

File tree

21 files changed

+111
-1
lines changed

21 files changed

+111
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ Or can be ignored by setting the `ignored_models` config
212212
],
213213
```
214214

215+
#### Ignore Models Properties
216+
217+
Models properties can be ignored by setting the `ignored_models_properties` config
218+
219+
```php
220+
'ignored_models_properties' => [
221+
App\Post::class => [ 'hash' ],
222+
],
223+
```
224+
215225
#### Magic `where*` methods
216226

217227
Eloquent allows calling `where<Attribute>` on your models, e.g. `Post::whereTitle(…)` and automatically translates this to e.g. `Post::where('title', '=', '…')`.
@@ -301,6 +311,7 @@ class MyCustomHook implements ModelHookInterface
301311
return;
302312
}
303313

314+
$command->unsetProperty('hash');
304315
$command->setProperty('custom', 'string', true, false, 'My custom property');
305316
$command->unsetMethod('method');
306317
$command->setMethod('method', $command->getMethodType($model, '\Some\Class'), ['$param']);

config/ide-helper.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@
155155

156156
],
157157

158+
/*
159+
|--------------------------------------------------------------------------
160+
| Models properties to ignore
161+
|--------------------------------------------------------------------------
162+
|
163+
| Define which properties should be ignored per model. The key of the array
164+
| is the canonical class name of the model and the values the properties
165+
| name, e.g. `Model::class => [ 'ignored_column' ]`
166+
|
167+
*/
168+
169+
'ignored_models_properties' => [
170+
171+
],
172+
158173
/*
159174
|--------------------------------------------------------------------------
160175
| Models hooks

src/Console/ModelsCommand.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,14 @@ public function getPropertiesFromTable($model)
489489
return;
490490
}
491491

492+
$ignoredProperties = array_values($this->laravel['config']->get('ide-helper.ignored_models_properties.' . get_class($model), []));
493+
492494
$this->setForeignKeys($schema, $table);
493495
foreach ($columns as $column) {
494496
$name = $column->getName();
497+
if(in_array($name, $ignoredProperties)){
498+
continue;
499+
}
495500
if (in_array($name, $model->getDates())) {
496501
$type = $this->dateClass;
497502
} else {
@@ -816,6 +821,13 @@ public function setProperty($name, $type = null, $read = null, $write = null, $c
816821
}
817822
}
818823

824+
public function unsetProperty($name)
825+
{
826+
unset($this->properties[$name]);
827+
828+
$this->unsetMethod(Str::camel('where_' . $name));
829+
}
830+
819831
public function setMethod($name, $type = '', $arguments = [], $comment = '')
820832
{
821833
$methods = array_change_key_case($this->methods, CASE_LOWER);
@@ -830,7 +842,7 @@ public function setMethod($name, $type = '', $arguments = [], $comment = '')
830842

831843
public function unsetMethod($name)
832844
{
833-
unset($this->methods[strtolower($name)]);
845+
unset($this->methods[$name]);
834846
}
835847

836848
public function getMethodType(Model $model, string $classType)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Attributes\Models\Simple
1212
*
1313
* @property integer $id
14+
* @property string $unset
1415
* @property string|null $name
1516
* @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery()
1617
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
1718
* @method static \Illuminate\Database\Eloquent\Builder|Simple query()
1819
* @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value)
20+
* @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value)
1921
* @mixin \Eloquent
2022
*/
2123
class Simple extends Model

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Comment\Models\Simple
1414
*
1515
* @property integer $id
16+
* @property string $unset
1617
* @property string $both_same_name I'm a getter
1718
* @property string $both_without_getter_comment
1819
* @property-read string $faker_comment
@@ -31,6 +32,7 @@
3132
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
3233
* @method static \Illuminate\Database\Eloquent\Builder|Simple query()
3334
* @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value)
35+
* @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value)
3436
* @mixin \Eloquent
3537
*/
3638
class Simple extends Model

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\CustomCollection\Models\Simple
1313
*
1414
* @property integer $id
15+
* @property string $unset
1516
* @property-read SimpleCollection|Simple[] $relationHasMany
1617
* @property-read int|null $relation_has_many_count
1718
* @method static SimpleCollection|static[] all($columns = ['*'])
@@ -20,6 +21,7 @@
2021
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
2122
* @method static \Illuminate\Database\Eloquent\Builder|Simple query()
2223
* @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value)
24+
* @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value)
2325
* @mixin \Eloquent
2426
*/
2527
class Simple extends Model

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Getter\Models\Simple
1212
*
1313
* @property integer $id
14+
* @property string $unset
1415
* @property-read int|null $attribute_return_type_int_or_null
1516
* @property-read array $attribute_returns_array
1617
* @property-read bool $attribute_returns_bool
@@ -35,6 +36,7 @@
3536
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
3637
* @method static \Illuminate\Database\Eloquent\Builder|Simple query()
3738
* @method static \Illuminate\Database\Eloquent\Builder|Simple whereId($value)
39+
* @method static \Illuminate\Database\Eloquent\Builder|Simple whereUnset($value)
3840
* @mixin \Eloquent
3941
*/
4042
class Simple extends Model

tests/Console/ModelsCommand/Ignored/Test.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@
77
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
88
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
99
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Ignored\Models\Ignored;
10+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Ignored\Models\NotIgnored;
11+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Ignored\Models\Simple;
1012

1113
class Test extends AbstractModelsCommand
1214
{
15+
1316
protected function getEnvironmentSetUp($app)
1417
{
1518
parent::getEnvironmentSetUp($app);
1619

1720
$app['config']->set('ide-helper.ignored_models', [
1821
Ignored::class,
1922
]);
23+
24+
$app['config']->set('ide-helper.ignored_models_properties', [
25+
NotIgnored::class => ['ignored'],
26+
]);
2027
}
2128

29+
2230
public function test(): void
2331
{
2432
$command = $this->app->make(ModelsCommand::class);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
/**
1010
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Ignored\Models\NotIgnored
1111
*
12+
* @property integer $id
1213
* @method static \Illuminate\Database\Eloquent\Builder|NotIgnored newModelQuery()
1314
* @method static \Illuminate\Database\Eloquent\Builder|NotIgnored newQuery()
1415
* @method static \Illuminate\Database\Eloquent\Builder|NotIgnored query()
16+
* @method static \Illuminate\Database\Eloquent\Builder|NotIgnored whereId($value)
1517
* @mixin \Eloquent
1618
*/
1719
class NotIgnored extends Model
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\ModelHooks\Hooks;
6+
7+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
8+
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
9+
use Illuminate\Database\Eloquent\Model;
10+
11+
class UnsetProperty implements ModelHookInterface
12+
{
13+
public function run(ModelsCommand $command, Model $model): void
14+
{
15+
$command->unsetProperty('unset');
16+
}
17+
}

0 commit comments

Comments
 (0)