Closed tortuetorche closed 6 years ago
@pmatseykanets Can you comment or review this pull request, please?
@tortuetorche With #20 it can be done in the builder callback here, but I'm hesitant rushing into doing so. One of the reasons there is no obvious ordering for $bindings
. Depending on what you're doing with the $query
in the callback you may need to prepend, append or completely reorder binding values.
Agreed, the binding values need to be manipulating with precautions.
But I think, it's the responsibility of the developer to handle correctly the $bindings
variable.
And this feature is only for advance usages, e.g. using faceted search.
So to handles correctly binding we can do this:
App\User::getModel()->searchableUsing()->extendQuery(function ($query, $bindings) {
return $query->addSelect('name')
->where('organization_name', $bindings->push('GitHub'))
->whereIn('role', [$bindings->push('admin'), $bindings->push('moderator')])
->where('gender', $bindings->push('male'))
->where('active', $bindings->push(true))
->whereYear('created_at', '>', $bindings->push(date('Y') - 2));
});
$users= App\User::search('Peter')->raw();
Or more simplier:
App\User::getModel()->searchableUsing()->extendQuery(function ($query, $bindings) {
$query->addSelect('name')
->where('organization_name', 'GitHub')
->whereIn('role', ['admin', 'moderator'])
->where('gender', 'male')
->where('active', true)
->whereYear('created_at', '>', date('Y') - 2);
// NOTICE: The code below could be moved to: https://github.com/tortuetorche/laravel-scout-postgres/blob/23ec3e43429dcbbd58762031e9d5de5ab8f3200f/src/PostgresEngine.php#L289
// and the $bindings variable could be removed from this Closure
foreach ($query->getBindings() as $binding) {
$bindings->push($binding);
}
return $query;
});
$users= App\User::search('Peter')->raw();
Closing in favor of https://github.com/pmatseykanets/laravel-scout-postgres/pull/21
Notice: This pull request is a refactoring of my initial pull request https://github.com/pmatseykanets/laravel-scout-postgres/pull/18 With the help of a
Closure
, which is more elegant than my previous attempt 😸Use case: