Each

0
Демонстрация метода each в коллекциях
app/Http/Controllers/ExampleController.php
                            
<?php
 
namespace App\Http\Controllers;
 
use App\Models\Example;
 
class ExampleController extends Controller
{
public function show($id)
{
$extensions = [
"jpg" => 0,
"png" => 0,
"gif" => 0,
];
 
$item = Example::findOrFail($id);
 
$item->images->each(function ($image) use (&$extensions) {
$extensions[$image->getExtension()]++;
});
 
return view("example.show", ["item" => $item, "extensions" => $extensions]);
}
}