diff --git a/app/Abstracts/Model.php b/app/Abstracts/Model.php index 55de88cd1..1986ab47e 100644 --- a/app/Abstracts/Model.php +++ b/app/Abstracts/Model.php @@ -2,6 +2,8 @@ namespace App\Abstracts; +use App\Events\Common\SearchStringApplied; +use App\Events\Common\SearchStringApplying; use App\Traits\DateTime; use App\Traits\Owners; use App\Traits\Tenants; @@ -115,9 +117,7 @@ abstract class Model extends Eloquent implements Ownable { $request = request(); - $search = $request->get('search'); - - $query->usingSearchString($search)->sortable($sort); + $query->usingSearchString()->sortable($sort); if ($request->expectsJson() && $request->isNotApi()) { return $query->get(); @@ -128,6 +128,15 @@ abstract class Model extends Eloquent implements Ownable return $query->paginate($limit); } + public function scopeUsingSearchString($query) + { + event(new SearchStringApplying($query)); + + $this->getSearchStringManager()->updateBuilder($query, request('search')); + + event(new SearchStringApplied($query)); + } + /** * Scope to export the rows of the current page filtered and sorted. * diff --git a/app/Events/Common/SearchStringApplied.php b/app/Events/Common/SearchStringApplied.php new file mode 100644 index 000000000..0a5f57d1e --- /dev/null +++ b/app/Events/Common/SearchStringApplied.php @@ -0,0 +1,20 @@ +query = $query; + } +} diff --git a/app/Events/Common/SearchStringApplying.php b/app/Events/Common/SearchStringApplying.php new file mode 100644 index 000000000..0c0d71c48 --- /dev/null +++ b/app/Events/Common/SearchStringApplying.php @@ -0,0 +1,20 @@ +query = $query; + } +} diff --git a/app/View/Components/SearchString.php b/app/View/Components/SearchString.php index 44354bac0..7859aeea8 100644 --- a/app/View/Components/SearchString.php +++ b/app/View/Components/SearchString.php @@ -90,7 +90,11 @@ class SearchString extends Component } if (isset($options['relationship'])) { - $column .= '.id'; + if (isset($options['foreign_key'])) { + $column .= '.' . $options['foreign_key']; + } else { + $column .= '.id'; + } } return $column; @@ -104,6 +108,10 @@ class SearchString extends Component $column = str_replace('_code', '', $column); } + if (!empty($options['translation'])) { + return $options['translation']; + } + if (!empty($options['key'])) { $column = $options['key']; }