I'd like to add a method to my Laravel Product
model which filters by name
attr and returns a collection of all matching products, here's what I've got:
Product.php
public function filterByName($query)
{
return $this->where('name','LIKE','%'.$query.'%')->get();
}
ProductController.php
$products = collect(new Product);
$products->filterByName($name);
What's the correct usage of this? Do I need to use a QueryFilter?