Skip to content

Commit 7507d24

Browse files
1. Add Query Builder
2. Add Eloquent 3. add multi search options 4. add multi ordering options 5. add multi select filter options
1 parent a2a255c commit 7507d24

File tree

14 files changed

+678
-121
lines changed

14 files changed

+678
-121
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
}
1111
],
1212
"minimum-stability": "stable",
13+
"prefer-stable": true,
1314
"require": {},
1415

1516
"autoload": {

src/DataGrid.php

Lines changed: 0 additions & 120 deletions
This file was deleted.

src/DataTableServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public function register()
2525
*/
2626
public function boot()
2727
{
28-
// dd('hello');
2928
//
3029
if ($this->app->runningInConsole()) {
3130
$this->commands([

src/Mysql/Eloquent/Eloquent.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
4+
namespace Yazan\DataTable\Mysql\Eloquent;
5+
6+
7+
trait Eloquent
8+
{
9+
use Helper;
10+
11+
protected $request;
12+
protected $namespace;
13+
14+
15+
16+
17+
public function __construct()
18+
{
19+
$this->request = request();
20+
$this->namespace = $this->model;
21+
22+
if(!isset($this->searchColumns) || !count($this->searchColumns)){
23+
24+
$this->searchColumns = [
25+
'normal' => ['id'],
26+
];
27+
}
28+
29+
30+
31+
}
32+
33+
34+
35+
36+
37+
38+
protected function create()
39+
{
40+
$this->model = $this->model::query();
41+
}
42+
43+
protected function with(){
44+
if(isset($this->with) && count($this->with))$this->model->with($this->with);
45+
}
46+
47+
48+
protected function search()
49+
{
50+
if(empty($this->request->search)) return;
51+
52+
$search = new Search($this->model);
53+
foreach ($this->searchColumns as $type => $columns):
54+
$search->{$type}($columns);
55+
endforeach;
56+
57+
}
58+
59+
protected function select()
60+
{
61+
if(empty($this->request->select) ) return;
62+
63+
$select = new Select($this->model);
64+
65+
foreach($this->request->select as $selectInput):
66+
67+
if(!empty($selectInput) && !empty($selectInput['value'])) $select->{$selectInput['type']}($selectInput);
68+
69+
70+
endforeach;
71+
72+
73+
74+
/*
75+
* select: {
76+
* Admins: {
77+
* type: relational
78+
* relation: "createdBy",
79+
* value: 1,
80+
* },
81+
* Places: {
82+
* type: normal
83+
* column:'id'
84+
* value: 1
85+
* }
86+
* }
87+
* */
88+
89+
90+
}
91+
92+
protected function orderBy()
93+
{
94+
$order = new Order($this->model, $this->namespace);
95+
$order->{$this->request->sort['type'] ?? 'normal'}($this->request->sort);
96+
}
97+
98+
protected function paginate()
99+
{
100+
return $this->model = $this->model->paginate($this->request->perPage, isset($this->columns) && count($this->columns) ? $this->columns : '*');
101+
102+
}
103+
protected function mapping(callable $callback)
104+
{
105+
$this->model = $this->model->toArray();
106+
$data = array_map(function($value) use (&$callback){
107+
108+
109+
return $callback($value);
110+
111+
}, $this->model['data']);
112+
113+
114+
$this->model['data'] = $data;
115+
116+
117+
}
118+
119+
public function render()
120+
{
121+
122+
$this->create();
123+
$this->with();
124+
$this->search();
125+
$this->select();
126+
$this->orderBy();
127+
$this->paginate();
128+
if($this->isMapping()) $this->reMapping();
129+
130+
131+
return $this->resultHandling();
132+
}
133+
134+
135+
}

src/Mysql/Eloquent/Helper.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
4+
namespace Yazan\DataTable\Mysql\Eloquent;
5+
6+
7+
trait Helper
8+
{
9+
public function isMapping()
10+
{
11+
return method_exists($this, 'reMapping');
12+
}
13+
14+
public function resultHandling()
15+
{
16+
return $this->isMapping() ?$this->model : $this->model->toArray();
17+
}
18+
19+
20+
}

src/Mysql/Eloquent/Order.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
4+
namespace Yazan\DataTable\Mysql\Eloquent;
5+
6+
7+
use Illuminate\Support\Facades\DB;
8+
9+
class Order
10+
{
11+
protected $model;
12+
protected $request;
13+
14+
public function __construct($model, $namespace)
15+
{
16+
$this->model = $model;
17+
$this->namespace = $namespace;
18+
$this->request = request();
19+
}
20+
21+
/*
22+
* select: {
23+
* Admins: {
24+
* type: relational
25+
* table: "admins",
26+
* foreign_key: created_by,
27+
* value: 1,
28+
* },
29+
* Places: {
30+
* type: normal
31+
* column:'id'
32+
* value: 1
33+
* }
34+
* }
35+
* */
36+
37+
public function normal($order)
38+
{
39+
40+
$this->model->orderBy($order['column'] ?? 'id', $order['sortDir'] ?? 'asc');
41+
42+
}
43+
44+
public function relational($order)
45+
{
46+
$table = $order['table'];
47+
$column = $order['column'];
48+
$foreignKey = $order['foreign_key'] ?? '';
49+
$modelTable = app($this->namespace)->getTable();
50+
$subQuery = DB::table($table)->select($column)->whereColumn("$table.id", "$modelTable.$foreignKey");
51+
52+
53+
$this->model->orderBy($subQuery, $order['sortDir'] ?? 'asc');
54+
55+
56+
}
57+
58+
public function translatable($order)
59+
{
60+
61+
$this->model->orderByTranslation($order['column'] ?? 'id', $order['sortDir'] ?? 'asc');
62+
63+
}
64+
}

0 commit comments

Comments
 (0)