1. Go to this page and download the library: Download gency/laravel-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/ */
gency / laravel-filterable example snippets
class User extends Model
{
use \Gency\Filterable\FilterableTrait;
protected $filterable = [
'keyword' => 'Keyword'
];
public function scopeFilterKeyword($query, $field, $arg) {
$query->where(function ($q) use ($arg) {
$q->where('name', $arg);
$q->orWhere('email', $arg);
$q->orWhere('phone', $arg);
});
return $query;
}
}
class User extends Model
{
...
public function scopeFilterNotKeyword($query, $field, $arg) {
return $query
->where('name', '!=', $arg)
->where('email', '!=', $arg)
->where('phone', '!=', $arg);
}
}
class Post extends Model
{
use \Gency\Filterable\FilterableTrait;
public function getFilterable () {
return [
'comment' => $this->comments()
];
}
public function comments() {
return $this->hasMany(Comment::class);
}
}
class Comment extends Model
{
use \Gency\Filterable\FilterableTrait;
public function getFilterable () {
return [
'created_at' => Filterable::Date
];
}
public function post() {
return $this->belongsTo(Post::class);
}
}
# Get all posts that have a comment in June 2017
$filter = [
'comment' => [
'created_at_MIN' => '2017-06-01',
'cerated_at_MAX' => '2017-07-01'
]
];
Post::filter($filter)->toSql()
// select * from "posts"
// inner join (
// select distinct "comments"."post_id" from "comments"
// where "created_at" >= ? and "created_at" <= ?
// ) as "comments_1" on "posts"."id" = "comments_1"."post_id"
class Post extends Model
{
use \Gency\Filterable\FilterableTrait;
public function getFilterable () {
return [
'comment' => $this->comments()
];
}
public function comments() {
return $this->hasMany(Comment::class);
}
}
class User extends Model
{
use \Gency\Filterable\FilterableTrait;
protected $filterable = [
'id' => Filterable::Integer
];
}
class Comment extends Model
{
use \Gency\Filterable\FilterableTrait;
public function getFilterable () {
return [
'created_at' => Filterable::Date,
'author' => $this->author()
];
}
public function post() {
return $this->belongsTo(Post::class);
}
public function author() {
return $this->belongsTo(User::class);
}
}
$filter1 = [
'comment' => [
'created_at_MIN' => '2017-06-01',
'created_at_MAX' => '2017-07-01'
]
];
$filter2 = [
'comment' => [
'author' => [
'id' => 1
]
]
];
Post::filter($filter1)->filter($filter2)->toSql()
// select * from "posts"
// inner join (
// select distinct "comments"."post_id"
// from "comments"
// inner join (
// select distinct "users"."id" as "author_id" from "users"
// where "id" = ?
// ) as "authors_1" on "comments"."author_id" = "authors_1"."author_id"
// where "created_at" >= ? and "created_at" <= ?
// ) as "comments_1" on "posts"."id" = "comments_1"."post_id"
class Post
{
use \Gency\Filterable\FilterableTrait;
protected $filterable = [
'body' => Filterable::FT
];
public function getFilterableFtTable ($field) {
return 'search';
};
public function getFilterableFtKeyName ($field) {
return 'post_id';
}
public function getFilterableFtVector ($field) {
return $field;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.