1. Go to this page and download the library: Download dmarte/filterable 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/ */
dmarte / filterable example snippets
// IN YOUR ELOQUENT MODEL
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use \Dmarte\Filterable\Filterable;
class Contact extends Model {
// Just, import the trait filterable
use Filterable;
}
// IN YOUR CONTROLLER
use App\Models\Contact;
use Illuminate\Support\Facades\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Controllers\Controller;
class ContactController extends Controller {
public function index(Request $request) : JsonResource{
// Just call the static function "filter" to perform the query.
return Contact::filter($request);
}
}
// IN YOUR MODEL
// just override the method that indicate
// the columns should be full-text filterable
/**
* @return array
*/
protected static function fullTextColumns(): array
{
return [
'name',
'description',
//...
];
}
// IN YOUR MODEL
protected function filterableQueries(): Collection
{
return collect([
'team_id' => fn(Builder $query, $column, $value) => $query->where($column, $value),
'emitted_at' => function (Builder $query, $column, $value) {
if (is_array($value)) {
return $query->whereBetween($column, $value);
}
return $query->where($column, $value);
},
]);
}
protected static function qualifiedResource(): string
{
return \App\Http\Resources\ModelResource::class;
}
// Add column filter that will be applied to all models.
$request->merge([
'team_id' => $request->user()->team_id,
]);
$engine = new FilterableMultiple(
models: [
Service::class,
Product::class,
],
request: $request
);
// Customize the query used for a given model
$engine->query(Service::class, function (Builder $query) {
$query->where('kind', 'service_custome_value');
});
// Return the collection
return $engine->get()
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.