sample model in laravel with query function code example
Example 1: laravel list of models
$path) {
$models = array_merge(collect(File::allFiles(base_path($path)))
->map(function ($item) use ($namespace) {
$path = $item->getRelativePathName();
return sprintf('\%s%s',
$namespace,
strtr(substr($path, 0, strrpos($path, '.')), '/', '\\'));
})
->filter(function ($class) {
$valid = false;
if (class_exists($class)) {
$reflection = new \ReflectionClass($class);
$valid = $reflection->isSubclassOf(\Illuminate\Database\Eloquent\Model::class) &&
!$reflection->isAbstract();
}
return $valid;
})
->values()
->toArray(), $models);
}
return $models;
}
Example 2: laravel model::query
// Eloquent's Model::query() returns the query builder
Model::where()->get();
// Is the same as
Model::query()->where()->get();
Model::query();
// Can be useful to add query conditions based on certain requirements
// See https://stackoverflow.com/questions/51517203/what-is-the-meaning-of-eloquents-modelquery