Skip to content

Commit 533a685

Browse files
[11.x] Add builder and collection to ModelInspector (#53565)
* add builder and collection to model inspector * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent b2d6991 commit 533a685

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/Illuminate/Database/Console/ShowModelCommand.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ public function handle(ModelInspector $modelInspector)
5151
return 1;
5252
}
5353

54-
$this->display(...$info);
54+
$this->display(
55+
$info['class'],
56+
$info['database'],
57+
$info['table'],
58+
$info['policy'],
59+
$info['attributes'],
60+
$info['relations'],
61+
$info['events'],
62+
$info['observers']
63+
);
5564

5665
return 0;
5766
}

src/Illuminate/Database/Eloquent/ModelInspector.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function __construct(Application $app)
5858
*
5959
* @param class-string<\Illuminate\Database\Eloquent\Model>|string $model
6060
* @param string|null $connection
61-
* @return array{"class": class-string<\Illuminate\Database\Eloquent\Model>, database: string, table: string, policy: string|null, attributes: \Illuminate\Support\Collection, relations: \Illuminate\Support\Collection, events: \Illuminate\Support\Collection, observers: \Illuminate\Support\Collection}
61+
* @return array{"class": class-string<\Illuminate\Database\Eloquent\Model>, database: string, table: string, policy: class-string|null, attributes: \Illuminate\Support\Collection, relations: \Illuminate\Support\Collection, events: \Illuminate\Support\Collection, observers: \Illuminate\Support\Collection, collection: class-string<\Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model>>, builder: class-string<\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>>}
6262
*
6363
* @throws BindingResolutionException
6464
*/
@@ -82,6 +82,8 @@ public function inspect($model, $connection = null)
8282
'relations' => $this->getRelations($model),
8383
'events' => $this->getEvents($model),
8484
'observers' => $this->getObservers($model),
85+
'collection' => $this->getCollectedBy($model),
86+
'builder' => $this->getBuilder($model),
8587
];
8688
}
8789

@@ -271,6 +273,30 @@ protected function getObservers($model)
271273
return collect($formatted);
272274
}
273275

276+
/**
277+
* Get the collection class being used by the model.
278+
*
279+
* @param \Illuminate\Database\Eloquent\Model $model
280+
* @return class-string<\Illuminate\Database\Eloquent\Collection>
281+
*/
282+
protected function getCollectedBy($model)
283+
{
284+
return $model->newCollection()::class;
285+
}
286+
287+
/**
288+
* Get the builder class being used by the model.
289+
*
290+
* @template TModel of \Illuminate\Database\Eloquent\Model
291+
*
292+
* @param TModel $model
293+
* @return class-string<\Illuminate\Database\Eloquent\Builder<TModel>>
294+
*/
295+
protected function getBuilder($model)
296+
{
297+
return $model->newQuery()::class;
298+
}
299+
274300
/**
275301
* Qualify the given model class base name.
276302
*

tests/Integration/Database/ModelInspectorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Illuminate\Tests\Integration\Database;
44

5+
use Illuminate\Database\Eloquent\Attributes\CollectedBy;
56
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
7+
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Database\Eloquent\Collection;
69
use Illuminate\Database\Eloquent\Concerns\HasUuids;
710
use Illuminate\Database\Eloquent\Model;
811
use Illuminate\Database\Eloquent\ModelInspector;
@@ -34,6 +37,8 @@ public function test_extracts_model_data()
3437
$extractor = new ModelInspector($this->app);
3538
$modelInfo = $extractor->inspect(ModelInspectorTestModel::class);
3639
$this->assertModelInfo($modelInfo);
40+
$this->assertSame(ModelInspectorTestModelEloquentCollection::class, $modelInfo['collection']);
41+
$this->assertSame(ModelInspectorTestModelBuilder::class, $modelInfo['builder']);
3742
}
3843

3944
public function test_command_returns_json()
@@ -175,10 +180,12 @@ private function assertAttributes($expectedAttributes, $actualAttributes)
175180
}
176181

177182
#[ObservedBy(ModelInspectorTestModelObserver::class)]
183+
#[CollectedBy(ModelInspectorTestModelEloquentCollection::class)]
178184
class ModelInspectorTestModel extends Model
179185
{
180186
use HasUuids;
181187

188+
protected static string $builder = ModelInspectorTestModelBuilder::class;
182189
public $table = 'model_info_extractor_test_model';
183190
protected $guarded = ['name'];
184191
protected $casts = ['nullable_date' => 'datetime', 'a_bool' => 'bool'];
@@ -201,3 +208,12 @@ public function created()
201208
{
202209
}
203210
}
211+
212+
class ModelInspectorTestModelEloquentCollection extends Collection
213+
{
214+
}
215+
216+
class ModelInspectorTestModelBuilder extends Builder
217+
{
218+
219+
}

0 commit comments

Comments
 (0)