Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel 8 model filter

<?php
  
$filters = EloquentFilters::make([
    new NameFilter($request->name)
]);
 
$products = Product::filter($filters)->get();


use PricecurrentLaravelEloquentFiltersAbstractEloquentFilter;
use IlluminateDatabaseEloquentBuilder;
 
class NameFilter extends AbstractEloquentFilter
{
    protected $name;
 
    public function __construct($name)
    {
        $this->name = $name;
    }
 
    public function apply(Builder $builder): Builder
    {
        return $query->where('name', 'like', "{$this->name}%");
    }
}

$filters = EloquentFilters::make([
    new NameFilter($request->user_name)
]);
 
$products = User::query()
    ->filter($filters)
    ->limit(10)
    ->latest()
    ->get();
Source by laravel-news.com #
 
PREVIOUS NEXT
Tagged: #laravel #model #filter
ADD COMMENT
Topic
Name
8+6 =