|
@@ -5,7 +5,10 @@ namespace Dcat\Admin\Grid\Column;
|
|
|
use Dcat\Admin\Grid\Column;
|
|
|
use Dcat\Admin\Grid\Model;
|
|
|
use Dcat\Admin\Support\Helper;
|
|
|
+use Dcat\Laravel\Database\WhereHasInServiceProvider;
|
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
|
+use Illuminate\Support\Arr;
|
|
|
+use Illuminate\Support\Str;
|
|
|
|
|
|
abstract class Filter implements Renderable
|
|
|
{
|
|
@@ -22,7 +25,7 @@ abstract class Filter implements Renderable
|
|
|
/**
|
|
|
* @var string
|
|
|
*/
|
|
|
- protected $getColumnName;
|
|
|
+ protected $columnName;
|
|
|
|
|
|
/**
|
|
|
* @var \Closure[]
|
|
@@ -81,7 +84,7 @@ abstract class Filter implements Renderable
|
|
|
*/
|
|
|
public function setColumnName(string $name)
|
|
|
{
|
|
|
- $this->getColumnName = $name;
|
|
|
+ $this->columnName = $name;
|
|
|
|
|
|
return $this;
|
|
|
}
|
|
@@ -93,7 +96,15 @@ abstract class Filter implements Renderable
|
|
|
*/
|
|
|
public function getColumnName()
|
|
|
{
|
|
|
- return $this->getColumnName ?: $this->parent->getName();
|
|
|
+ return str_replace(['.', '->'], '_', $this->getOriginalColumnName());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function getOriginalColumnName()
|
|
|
+ {
|
|
|
+ return $this->columnName ?: $this->parent->getName();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -118,6 +129,48 @@ abstract class Filter implements Renderable
|
|
|
return request($this->getQueryName(), $default);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @param mixed $model
|
|
|
+ * @param string $query
|
|
|
+ * @param mixed array $params
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ protected function withQuery($model, string $query, array $params)
|
|
|
+ {
|
|
|
+ $column = $this->getOriginalColumnName();
|
|
|
+
|
|
|
+ if (! Str::contains($column, '.')) {
|
|
|
+ $model->$query($column, ...$params);
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->withRelationQuery($model, $query, $params);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param mixed $model
|
|
|
+ * @param string $query
|
|
|
+ * @param mixed ...$params
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ protected function withRelationQuery($model, string $query, array $params)
|
|
|
+ {
|
|
|
+ $column = $this->getOriginalColumnName();
|
|
|
+
|
|
|
+ $relation = substr($column, 0, strrpos($column, '.'));
|
|
|
+ array_unshift($params, Arr::last(explode('.', $column)));
|
|
|
+
|
|
|
+ // 增加对whereHasIn的支持
|
|
|
+ $method = class_exists(WhereHasInServiceProvider::class) ? 'whereHasIn' : 'whereHas';
|
|
|
+
|
|
|
+ $model->$method($relation, function ($relation) use ($params, $query) {
|
|
|
+ $relation->$query(...$params);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Add reset button.
|
|
|
*/
|