PHP code example of usermp / laravel-filter

1. Go to this page and download the library: Download usermp/laravel-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/ */

    

usermp / laravel-filter example snippets


    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;
    use Usermp\LaravelFilter\Traits\Filterable; // Correct path to the Trait

    class YourModel extends Model
    {
        use Filterable;

        // (Optional) Override the main request key containing the filters.
        // protected string $filterRequestKeyOverride = 'my_filters'; // Defaults to 'filter'

        // Attributes of this model that can be filtered
        protected $filterable = [
            'attribute1',
            'attribute2',
            'status',
            'created_at',
            // Add other filterable attributes
        ];

        // Names of relations whose attributes can be filtered
        protected $filterableRelations = [
            'relation1', // Example: for filtering relation1.name
            'user',
            // Add other filterable relations
        ];
    }
    

namespace App\Http\Controllers;

use App\Models\YourModel;
use Illuminate\Http\Request; // Inject the Request object

class YourModelController extends Controller
{
    public function index(Request $request)
    {
        $results = YourModel::query()
            ->filter($request) // Pass the entire Request object
            ->paginate();

        return response()->json($results);
    }
}