Filter

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