1. Go to this page and download the library: Download ahmedabdo/searchable 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/ */
ahmedabdo / searchable example snippets
// model
use Abdo\Searchable\Searchable;
use Abdo\Searchable\Attributes\SearchAdd;
use Abdo\Searchable\Attributes\SearchColumns;
class User extends Authenticatable
{
use Searchable;
#[SearchColumns]
public $searchable = [
"columns" => [
"name",
"email",
"role.name",
"created_at"
],
"eager" => [
"role"
]
];
#[SearchAdd("created_at")]
public function searchByCreatedAtDayName(Builder $q, string $searchWord) {
$q->orWhereRaw("DAYNAME(created_at) like ?", ["%" . $searchWord . "%"]);
}
}
// usage
User::search($searchWord)->get();
// ?<not-colname>=<operator, default:"=">|<value>
// To retrieve the filter value from the query string, you can utilize the "filterParam" helper
User::filter(["column_name" => filterParam("not_column_name")])->get();
// Note that if the rest of the query string parameter names match the column names
// you can employ a similar approach.
User::filter(["column_name" => filterParam("not_column_name")])->filter()->get();
// using or mode
User::filter(mode: Mode::OR)->get();
// using and mode
User::filter(mode: Mode::AND)->get();
User::filter()->get();
ColumnConfigraution::registerOperator("sp_is_null", function (Builder $builder, string $column, string $word) {
return $builder->whereIsNull($column)->orWhere($column, 0);
});
//after defining your custom operator you can use them like this
//?status=sp_is_null|