PHP code example of putheng / filter

1. Go to this page and download the library: Download putheng/filter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

putheng / filter example snippets


Putheng\Filter\FilterServiceProvider::class,

use App\Filters\Filter\DifficultyFilter;

protected $filters = [
    'difficulty' => DifficultyFilter::class,
];

namespace App\Filters\Filter;

use Putheng\Filter\FilterAbstract;
use Illuminate\Database\Eloquent\Builder;

class DifficultyFilter extends FilterAbstract
{
    /**
     * Mappings for database values.
     * 
     * Map b on query string map to beginner column on database
     * 
     * 'b' => 'beginner'
     * 'beginner' => 'beginner'
     * 
     * @return array
     */
    public function mappings()
    {
        return [
            'b' => 'beginner',
            'intermediate' => 'intermediate',
            'advanced' => 'advanced',
        ];
    }

    /**
     * Filter by course difficulty.
     *
     * @param  string $access
     * @return Illuminate\Database\Eloquent\Builder
     */
    public function filter(Builder $builder, $value)
    {
        $value = $this->resolveFilterValue($value);

        if ($value === null) {
            return $builder;
        }

        return $builder->where('difficulty', $value);
    }
}

namespace App;
// use filter class we just generated
use App\Filters\CourseFilters;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Course extends Model
{
    public function scopeFilter(Builder $builder, Request $request, array $filters = [])
    {
        return (new CourseFilters($request))->add($filters)->filter($builder);
    }
}


public function subjects()
{
    return $this->morphToMany(Subject::class, 'subjectable');
}

return $builder->whereHas('subjects', function (Builder $builder) use ($value) {
    $builder->where('slug', $value);
});

namespace App\Http\Controllers;

use App\Course;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class CourseController extends Controller
{
    public function index(Request $request)
    {
        $filter = Course::filter($request)->get();
    }
}