1 /**
2 * The attributes that are mass assignable.
3 */
4 protected $fillable = [
5 'title',
6 'slug',
7 'body',
8 'image',
9 'published',
10 'comments_open'
11 ];
1<?php
2// found link from https://stackoverflow.com/questions/34053585/how-do-i-get-a-list-of-all-models-in-laravel
3// and then finally found on https://gist.github.com/mohammad425/231242958edb640601108bdea7bcf9ac
4function getAllModels(): array
5{
6 $composer = json_decode(file_get_contents(base_path('composer.json')), true);
7 $models = [];
8 foreach ((array)data_get($composer, 'autoload.psr-4') as $namespace => $path) {
9 $models = array_merge(collect(File::allFiles(base_path($path)))
10 ->map(function ($item) use ($namespace) {
11 $path = $item->getRelativePathName();
12 return sprintf('\%s%s',
13 $namespace,
14 strtr(substr($path, 0, strrpos($path, '.')), '/', '\\'));
15 })
16 ->filter(function ($class) {
17 $valid = false;
18 if (class_exists($class)) {
19 $reflection = new \ReflectionClass($class);
20 $valid = $reflection->isSubclassOf(\Illuminate\Database\Eloquent\Model::class) &&
21 !$reflection->isAbstract();
22 }
23 return $valid;
24 })
25 ->values()
26 ->toArray(), $models);
27 }
28 return $models;
29}
1all() is a static method on the Eloquent\Model.
2All it does is create a new query object and call get() on it.
3With all(), you cannot modify the query performed at all
4(except you can choose the columns to select by passing them as parameters).
5get() is a method on the Eloquent\Builder object.