Merge pull request #2241 from burakcakirel/make-search-and-filters-extendable
Allow to add Module's fields to the search and filter
This commit is contained in:
commit
e32091e965
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Abstracts;
|
namespace App\Abstracts;
|
||||||
|
|
||||||
|
use App\Events\Common\SearchStringApplied;
|
||||||
|
use App\Events\Common\SearchStringApplying;
|
||||||
use App\Traits\DateTime;
|
use App\Traits\DateTime;
|
||||||
use App\Traits\Owners;
|
use App\Traits\Owners;
|
||||||
use App\Traits\Sources;
|
use App\Traits\Sources;
|
||||||
@ -116,9 +118,7 @@ abstract class Model extends Eloquent implements Ownable
|
|||||||
{
|
{
|
||||||
$request = request();
|
$request = request();
|
||||||
|
|
||||||
$search = $request->get('search');
|
$query->usingSearchString()->sortable($sort);
|
||||||
|
|
||||||
$query->usingSearchString($search)->sortable($sort);
|
|
||||||
|
|
||||||
if ($request->expectsJson() && $request->isNotApi()) {
|
if ($request->expectsJson() && $request->isNotApi()) {
|
||||||
return $query->get();
|
return $query->get();
|
||||||
@ -129,6 +129,15 @@ abstract class Model extends Eloquent implements Ownable
|
|||||||
return $query->paginate($limit);
|
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.
|
* Scope to export the rows of the current page filtered and sorted.
|
||||||
*
|
*
|
||||||
|
20
app/Events/Common/SearchStringApplied.php
Normal file
20
app/Events/Common/SearchStringApplied.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Events\Common;
|
||||||
|
|
||||||
|
use App\Abstracts\Event;
|
||||||
|
|
||||||
|
class SearchStringApplied extends Event
|
||||||
|
{
|
||||||
|
public $query;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new event instance.
|
||||||
|
*
|
||||||
|
* @param $query
|
||||||
|
*/
|
||||||
|
public function __construct($query)
|
||||||
|
{
|
||||||
|
$this->query = $query;
|
||||||
|
}
|
||||||
|
}
|
20
app/Events/Common/SearchStringApplying.php
Normal file
20
app/Events/Common/SearchStringApplying.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Events\Common;
|
||||||
|
|
||||||
|
use App\Abstracts\Event;
|
||||||
|
|
||||||
|
class SearchStringApplying extends Event
|
||||||
|
{
|
||||||
|
public $query;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new event instance.
|
||||||
|
*
|
||||||
|
* @param $query
|
||||||
|
*/
|
||||||
|
public function __construct($query)
|
||||||
|
{
|
||||||
|
$this->query = $query;
|
||||||
|
}
|
||||||
|
}
|
@ -90,7 +90,11 @@ class SearchString extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isset($options['relationship'])) {
|
if (isset($options['relationship'])) {
|
||||||
$column .= '.id';
|
if (isset($options['foreign_key'])) {
|
||||||
|
$column .= '.' . $options['foreign_key'];
|
||||||
|
} else {
|
||||||
|
$column .= '.id';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $column;
|
return $column;
|
||||||
@ -104,6 +108,10 @@ class SearchString extends Component
|
|||||||
$column = str_replace('_code', '', $column);
|
$column = str_replace('_code', '', $column);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty($options['translation'])) {
|
||||||
|
return $options['translation'];
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($options['key'])) {
|
if (!empty($options['key'])) {
|
||||||
$column = $options['key'];
|
$column = $options['key'];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user