Skip to content

Commit

Permalink
修改Grid With相关
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallRuralDog committed Mar 19, 2020
1 parent 97a5830 commit 3f294c8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 28 deletions.
48 changes: 34 additions & 14 deletions src/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Relations;
use Illuminate\Support\Str;
use SmallRuralDog\Admin\Components\Component;
use SmallRuralDog\Admin\Grid\Actions;
use SmallRuralDog\Admin\Grid\Column;
Expand Down Expand Up @@ -35,8 +37,8 @@ class Grid extends Component implements \JsonSerializable
*/
protected $columns = [];
protected $rows;
public $columnAttributes = [];
protected $withs = [];
protected $columnAttributes = [];


protected $keyName = 'id';
protected $selection = false;
Expand All @@ -47,7 +49,7 @@ class Grid extends Component implements \JsonSerializable
private $toolbars;


public function __construct(Eloquent $model, Closure $builder = null)
public function __construct(Eloquent $model)
{
$this->attributes = new Attributes();
$this->dataUrl = request()->getUri();
Expand Down Expand Up @@ -76,24 +78,15 @@ public function model()
}


/**
* 获取with
* @return array
*/
public function getWiths(): array
{
return $this->withs;
}

/**
*设置with
* @param array $withs
* @return $this
* @deprecated
*/
public function with(array $withs)
{
$this->withs = $withs;

$this->model()->with($withs);
return $this;
}

Expand Down Expand Up @@ -131,6 +124,10 @@ public function tree($tree = true)
*/
public function column($name, $label = '', $columnKey = null)
{
if (Str::contains($name, '.')) {
$this->addRelationColumn($name, $label);
}

return $this->addColumn($name, $label, $columnKey);
}

Expand All @@ -148,6 +145,29 @@ protected function addColumn($name = '', $label = '', $columnKey = null)
return $column;
}

/**
* Add a relation column to grid.
*
* @param string $name
* @param string $label
*
* @return $this|bool|Column
*/
protected function addRelationColumn($name, $label = '')
{
list($relation, $column) = explode('.', $name);

$model = $this->model()->eloquent();


if (!method_exists($model, $relation) || !$model->{$relation}() instanceof Relations\Relation) {
} else {
$this->model()->with($relation);
}


}

/**
* @param Column[] $columns
* @deprecated
Expand Down
53 changes: 39 additions & 14 deletions src/Grid/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use SmallRuralDog\Admin\Grid;

Expand All @@ -24,6 +26,12 @@ class Model
*/
protected $model;


/**
* @var EloquentModel |Builder
*/
protected $sModel;

/**
* @var EloquentModel|Builder
*/
Expand Down Expand Up @@ -96,6 +104,7 @@ class Model
public function __construct(EloquentModel $model, Grid $grid = null)
{
$this->model = $model;
$this->sModel = $model;
$this->originalModel = $model;
$this->grid = $grid;
$this->queries = collect();
Expand Down Expand Up @@ -172,19 +181,34 @@ protected function setPaginate()
$this->queries->push($query);
}

/**
* 设置预加载
*/
protected function setWith()

public function with($relations)
{
$with = $this->grid->getWiths();
if (is_array($relations)) {
if (Arr::isAssoc($relations)) {
$relations = array_keys($relations);
}

if ($with) $this->queries->push([
'method' => 'with',
'arguments' => $with,
]);
$this->eagerLoads = array_merge($this->eagerLoads, $relations);
}

if (is_string($relations)) {
if (Str::contains($relations, '.')) {
$relations = explode('.', $relations)[0];
}

if (Str::contains($relations, ':')) {
$relations = explode(':', $relations)[0];
}

if (in_array($relations, $this->eagerLoads)) {
return $this;
}

$this->eagerLoads[] = $relations;
}

return $this->__call('with', (array)$relations);
}


Expand Down Expand Up @@ -313,22 +337,23 @@ public function get()
if ($this->model instanceof LengthAwarePaginator) {
return $this->model;
}

if ($this->relation) {
$this->model = $this->relation->getQuery();
}
$this->setWith();

$this->setSort();
$this->setPaginate();

//dd($this->queries);

$this->queries->unique()->each(function ($query) {
$this->model = call_user_func_array([$this->model, $query['method']], $query['arguments']);
});


$data = $this->model;

if ($this->model instanceof Collection) {
return $this->model;
return $data;
}

if ($this->model instanceof LengthAwarePaginator) {
Expand All @@ -337,7 +362,7 @@ public function get()
'per_page' => $this->model->perPage(),
'last_page' => $this->model->lastPage(),
'total' => $this->model->total(),
'data' => $this->displayData($this->model->getCollection())
'data' => $this->displayData($data->items())
];
}

Expand Down

0 comments on commit 3f294c8

Please sign in to comment.