all() in laravel code example
Example 1: laravel fillable
protected $fillable = [
'title',
'slug',
'body',
'image',
'published',
'comments_open'
];
Example 2: laravel list of models
<?php
function getAllModels(): array
{
$composer = json_decode(file_get_contents(base_path('composer.json')), true);
$models = [];
foreach ((array)data_get($composer, 'autoload.psr-4') as $namespace => $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 3: laravel eloquent fill
$flight->fill(['name' => 'Flight 22']);
Example 4: all() in laravel
all() is a static method on the Eloquent\Model.
All it does is create a new query object and call get() on it.
With all(), you cannot modify the query performed at all
(except you can choose the columns to select by passing them as parameters).
get() is a method on the Eloquent\Builder object.