Skip to content

Commit 851c72b

Browse files
Update QueryBuilder and Eloquent
1. Change create file command 2. Add Custom Query method to Eloquent 3. Add Translatable relations to Eloquent 4. Add Translatable to search methods
1 parent a2dac12 commit 851c72b

File tree

6 files changed

+64
-31
lines changed

6 files changed

+64
-31
lines changed

src/Commands/DataTableCommand.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DataTableCommand extends Command
1919
*
2020
* @var string
2121
*/
22-
protected $description = 'Create a new Data Grid class' ;
22+
protected $description = 'Create a new Data Grid class';
2323

2424
/**
2525
* Create a new command instance.
@@ -38,36 +38,30 @@ public function __construct()
3838
*/
3939
public function handle()
4040
{
41-
4241
$content = "<?php
4342
44-
4543
namespace App\DataGrid;
4644
47-
use Yazan\DataTable\DataGrid;
45+
use Yazan\DataTable\Mysql\Eloquent\Eloquent;
4846
49-
class {$this->argument('name')} extends DataGrid
47+
class {$this->argument('name')}
5048
{
51-
49+
use Eloquent;
5250
public \$model = \"\";
53-
54-
5551
}
5652
";
57-
if(!File::isDirectory('app/DataGrid')) File::makeDirectory('app/DataGrid');
58-
59-
60-
6153

54+
if (!File::isDirectory('app/DataGrid')) {
55+
File::makeDirectory('app/DataGrid');
56+
}
6257

63-
if(File::exists("app/DataGrid/{$this->argument('name')}.php")){
58+
$filePath = "app/DataGrid/{$this->argument('name')}.php";
6459

65-
$this->error('this file is exist');
66-
}else{
67-
File::put("app/DataGrid/{$this->argument('name')}.php", $content);
60+
if (File::exists($filePath)) {
61+
$this->error('This file already exists: ' . $filePath);
62+
} else {
63+
File::put($filePath, $content);
64+
$this->info('File created successfully: ' . $filePath);
6865
}
69-
//
70-
71-
// return 'hello';
7266
}
7367
}

src/Mysql/Eloquent/Search.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,22 @@ public function translatable($columns)
6262

6363
}
6464

65+
public function translatableRelations($relations)
66+
{
67+
68+
$search = $this->request->search;
69+
foreach ($relations as $relation => $columns):
70+
71+
$this->model->orWhereHas($relation, function ($query) use ($columns, $search) {
72+
73+
foreach ($columns as $column):
74+
$query->whereTranslation( $column, $search);
75+
endforeach;
76+
77+
});
78+
79+
endforeach;
80+
81+
}
82+
6583
}

src/Mysql/Eloquent/Select.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public function normal($select)
3737

3838
}
3939

40+
public function translatable($select)
41+
{
42+
$this->model->whereTranslation($select['searchColumn'] ?? 'id', $select['value']);
43+
44+
}
45+
4046
public function relational($select)
4147
{
4248

@@ -50,13 +56,7 @@ public function relational($select)
5056

5157
}
5258

53-
public function translatable($select)
54-
{
55-
$this->model->whereTranslation($select['searchColumn'] ?? 'id', $select['value']);
56-
57-
}
58-
59-
public function relationalTranslatable($select)
59+
public function translatableRelations($select)
6060
{
6161

6262
$this->model->WhereHas($select['relation'], function ($query) use ($select) {

src/Mysql/QueryBuilder/Helper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ public function isMapping()
1111
return method_exists($this, 'reMapping');
1212
}
1313

14+
public function isCustomQuery()
15+
{
16+
return method_exists($this, 'setCustomQuery');
17+
}
18+
1419
public function resultHandling()
1520
{
1621
return $this->isMapping() ?$this->model : $this->model->toArray();

src/Mysql/QueryBuilder/QueryBuilder.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function join(){
4646
if(isset($this->join) && count($this->join))
4747

4848
foreach ($this->join as $key => $value):
49-
$this->model->join($key, ...$value);
49+
$this->model->leftJoin($key, ...$value);
5050
endforeach;
5151

5252
}
@@ -107,6 +107,11 @@ protected function paginate()
107107
return $this->model = $this->model->paginate($this->request->perPage);
108108

109109
}
110+
111+
protected function runCustomQuery(callable $callback)
112+
{
113+
$callback($this->model);
114+
}
110115
protected function mapping(callable $callback)
111116
{
112117
$this->model = $this->model->toArray();
@@ -130,14 +135,14 @@ protected function mapping(callable $callback)
130135
public function render()
131136
{
132137

133-
$this->create();
138+
$this->create();
134139
$this->columns();
135140
$this->join();
136141
$this->search();
137142
$this->select();
143+
if($this->isCustomQuery()) $this->setCustomQuery();
138144
$this->orderBy();
139145
$this->paginate();
140-
141146
if($this->isMapping()) $this->reMapping();
142147

143148

src/Mysql/QueryBuilder/Select.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public function normal($select)
3737

3838
}
3939

40+
public function translatable($select)
41+
{
42+
$this->model->where($select['searchColumn'], $select['value']);
43+
44+
}
45+
4046
public function relational($select)
4147
{
4248

@@ -46,9 +52,14 @@ public function relational($select)
4652

4753
}
4854

49-
public function translatable($select)
55+
public function translatableRelations($select)
5056
{
51-
$this->model->where($select['searchColumn'], $select['value']);
57+
58+
$this->model->WhereHas($select['relation'], function ($query) use ($select) {
59+
60+
$query->whereTranslation($select['searchColumn'] ?? 'id', $select['value']);
61+
62+
});
5263

5364
}
5465

0 commit comments

Comments
 (0)