Skip to content

Commit d5de2d3

Browse files
committed
Testing new feature
1 parent 659445d commit d5de2d3

18 files changed

+542
-358
lines changed

Diff for: app/Http/Controllers/UserController.php

+20-52
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,34 @@ public function index(Request $request)
1414
$query = User::eloquentQuery(
1515
$request->input('column'),
1616
$request->input('dir'),
17-
$request->input('search')
17+
$request->input('search'),
18+
[
19+
"role",
20+
]
1821
);
1922

20-
$isAdmin = $request->input('isAdmin');
23+
// dd($query->toSql());
24+
25+
$isActive = $request->input('isActive');
2126

22-
if (isset($isAdmin) && ! empty($isAdmin)) {
23-
$query->where("type", $isAdmin);
27+
if (isset($isActive)) {
28+
$query->where("is_active", $isActive);
2429
}
2530

26-
$data = $query->with("role")->paginate($request->input('length'));
31+
$data = $query->paginate($request->input('length'));
2732

2833
return new DataTableCollectionResource($data);
2934
}
3035

3136
public function queryBuilder(Request $request)
3237
{
3338
$searchValue = $request->input('search');
39+
$orderBy = $request->input('column');
40+
$orderBydir = $request->input("dir");
41+
$length = $request->input('length');
3442

35-
$query = User::queryBuilderQuery(
36-
$request->input('column'),
37-
$request->input('dir'),
38-
$searchValue
39-
)->join('roles', 'roles.id', '=', 'users.role_id')
43+
$data = \DB::table('users')
44+
->join('roles', 'roles.id', '=', 'users.role_id')
4045
->join('departments', 'departments.id', '=', 'roles.department_id')
4146
->select(
4247
'roles.name as role_name',
@@ -46,16 +51,12 @@ public function queryBuilder(Request $request)
4651
'users.email',
4752
'departments.name as department_name'
4853
)
54+
->where("users.name", "LIKE", "%$searchValue%")
55+
->orWhere('users.email', "LIKE", "%$searchValue%")
4956
->orWhere('roles.name', "LIKE", "%$searchValue%")
50-
->orWhere('departments.name', "LIKE", "%$searchValue%");
51-
52-
$isAdmin = $request->input('isAdmin');
53-
54-
if (isset($isAdmin) && ! empty($isAdmin)) {
55-
$query->where("type", $isAdmin);
56-
}
57-
58-
$data = $query->paginate($request->input('length'));
57+
->orWhere('departments.name', "LIKE", "%$searchValue%")
58+
->orderBy($orderBy, $orderBydir)
59+
->paginate($length);
5960

6061
return new DataTableCollectionResource($data);
6162
}
@@ -65,37 +66,4 @@ public function search(Request $request)
6566
$searchValue = $request->input('search');
6667
return User::where("name", "like", "%$searchValue%")->get();
6768
}
68-
69-
public function pivot(Request $request)
70-
{
71-
$searchValue = $request->input('search');
72-
$orderBy = $request->input('column', 'id');
73-
$orderByDir = $request->input('dir', 'asc');
74-
$length = $request->input('length');
75-
76-
$query = User::eloquentQuery(
77-
$orderBy,
78-
$orderByDir,
79-
$searchValue
80-
);
81-
82-
if ($searchValue) {
83-
$query = $query->whereHas('roles', function ($q) use ($searchValue) {
84-
$q->orWhere('roles.name', "like", "%$searchValue%");
85-
});
86-
}
87-
88-
$data = $query->with('roles')->paginate($length);
89-
90-
return new DataTableCollectionResource($data);
91-
}
92-
93-
public function checkForColumnNesting($column)
94-
{
95-
if (count(explode(".", $column)) > 1) {
96-
return $column;
97-
} else {
98-
return $this->getTable() . ".$column";
99-
}
100-
}
10169
}

Diff for: app/User.php

+67-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Notifications\Notifiable;
66
use Illuminate\Contracts\Auth\MustVerifyEmail;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
78
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
89
use Illuminate\Database\Eloquent\Relations\BelongsTo;
910
use Illuminate\Foundation\Auth\User as Authenticatable;
@@ -15,24 +16,74 @@ class User extends Authenticatable
1516

1617
protected $dataTableColumns = [
1718
'id' => [
18-
'searchable' => false,
19+
'searchable' => true,
20+
'orderable' => true,
1921
],
2022
'name' => [
2123
'searchable' => true,
24+
'orderable' => true,
2225
],
2326
'email' => [
2427
'searchable' => true,
28+
'orderable' => true,
2529
],
2630
'is_active' => [
2731
'searchable' => true,
32+
'orderable' => true,
2833
],
2934
'cost' => [
3035
'searchable' => true,
31-
]
36+
'orderable' => true,
37+
],
3238
];
3339

34-
protected $dataTableJoins = [
35-
"roles"
40+
protected $dataTableRelationships = [
41+
"belongsTo" => [
42+
'role' => [
43+
"model" => \App\Role::class,
44+
'foreign_key' => 'role_id',
45+
'columns' => [
46+
'name' => [
47+
'searchable' => true,
48+
'orderable' => true,
49+
],
50+
],
51+
],
52+
],
53+
"hasMany" => [
54+
'usernames' => [
55+
"model" => \App\Username::class,
56+
'foreign_key' => 'user_id',
57+
'columns' => [
58+
'name' => [
59+
'searchable' => true,
60+
'orderable' => true,
61+
],
62+
],
63+
],
64+
],
65+
// "belongsToMany" => [
66+
// 'roles' => [
67+
// "model" => \App\Role::class,
68+
// "foreign_key" => "role_id",
69+
// "pivot" => [
70+
// "table_name" => "role_user",
71+
// "primary_key" => "id",
72+
// "foreign_key" => "role_id",
73+
// "local_key" => "user_id",
74+
// ],
75+
// "subOrder" => [
76+
// "order_by" => "roles.name",
77+
// "order_dir" => "asc",
78+
// ],
79+
// 'columns' => [
80+
// 'name' => [
81+
// 'searchable' => true,
82+
// 'orderable' => true,
83+
// ]
84+
// ],
85+
// ],
86+
// ]
3687
];
3788

3889
/**
@@ -62,13 +113,23 @@ class User extends Authenticatable
62113
'email_verified_at' => 'datetime',
63114
];
64115

65-
public function role() : BelongsTo
116+
public function role()
66117
{
67118
return $this->belongsTo(\App\Role::class);
68119
}
69120

70-
public function roles() : BelongsToMany
121+
public function workZone()
122+
{
123+
return $this->belongsTo(\App\WorkZone::class);
124+
}
125+
126+
public function roles()
71127
{
72128
return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');
73129
}
130+
131+
public function usernames()
132+
{
133+
return $this->hasMany(\App\Username::class);
134+
}
74135
}

Diff for: app/Username.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7+
8+
class Username extends Model
9+
{
10+
11+
}

Diff for: app/WorkZone.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7+
8+
class WorkZone extends Model
9+
{
10+
protected $table = "work_zones";
11+
}

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"barryvdh/laravel-cors": "^0.11.3",
1313
"facade/ignition": "^1.4",
1414
"fideloper/proxy": "^4.0",
15-
"jamesdordoy/laravelvuedatatable": "^1.2",
15+
"jamesdordoy/laravelvuedatatable": "^1.3",
1616
"laravel/framework": "6.0.*",
1717
"laravel/tinker": "^1.0"
1818
},

0 commit comments

Comments
 (0)