Skip to content

Commit 659445d

Browse files
committed
Updates to examples.
1 parent 52de206 commit 659445d

File tree

12 files changed

+1059
-688
lines changed

12 files changed

+1059
-688
lines changed

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

+35-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\User;
6+
use App\Role;
67
use Illuminate\Http\Request;
78
use JamesDordoy\LaravelVueDatatable\Http\Resources\DataTableCollectionResource;
89

@@ -35,10 +36,7 @@ public function queryBuilder(Request $request)
3536
$request->input('column'),
3637
$request->input('dir'),
3738
$searchValue
38-
);
39-
40-
$query
41-
->join('roles', 'roles.id', '=', 'users.role_id')
39+
)->join('roles', 'roles.id', '=', 'users.role_id')
4240
->join('departments', 'departments.id', '=', 'roles.department_id')
4341
->select(
4442
'roles.name as role_name',
@@ -67,4 +65,37 @@ public function search(Request $request)
6765
$searchValue = $request->input('search');
6866
return User::where("name", "like", "%$searchValue%")->get();
6967
}
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+
}
70101
}

Diff for: app/User.php

+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\BelongsToMany;
78
use Illuminate\Database\Eloquent\Relations\BelongsTo;
89
use Illuminate\Foundation\Auth\User as Authenticatable;
910
use JamesDordoy\LaravelVueDatatable\Traits\LaravelVueDatatableTrait;
@@ -65,4 +66,9 @@ public function role() : BelongsTo
6566
{
6667
return $this->belongsTo(\App\Role::class);
6768
}
69+
70+
public function roles() : BelongsToMany
71+
{
72+
return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');
73+
}
6874
}

Diff for: database/factories/RolesFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$factory->define(Role::class, function (Faker $faker) {
99

1010
return [
11-
'department_id' => factory(App\Department::class)->create()->id,
11+
// 'department_id' => factory(App\Department::class)->create()->id,
1212
'name' => $faker->randomElement(['User', 'Staff', 'Admin']),
1313
'handle' => $faker->randomElement(['user', 'staff', 'admin']),
1414
];

Diff for: database/factories/UserFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
$factory->define(User::class, function (Faker $faker) {
2020
return [
2121
'name' => $faker->name,
22-
'role_id' => factory(App\Role::class)->create()->id,
22+
// 'role_id' => factory(App\Role::class)->create()->id,
2323
'type' => $faker->randomElement(['admin', 'staff']),
2424
'email' => $faker->unique()->safeEmail,
2525
'email_verified_at' => now(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateRoleUserTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('role_user', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->bigInteger('role_id')->unsigned();
19+
$table->foreign('role_id')->references('id')->on('roles');
20+
$table->bigInteger('user_id')->unsigned();
21+
$table->foreign('user_id')->references('id')->on('users');
22+
$table->timestamps();
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::dropIfExists('role_user');
34+
}
35+
}

Diff for: database/seeds/DatabaseSeeder.php

+47-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,52 @@ class DatabaseSeeder extends Seeder
1111
*/
1212
public function run()
1313
{
14-
factory(\App\User::class, 50)->create();
14+
$customerService = factory(\App\Department::class)->create([
15+
'name' => 'Customer Service',
16+
'handle' => 'customer-service',
17+
]);
18+
19+
$technology = factory(\App\Department::class)->create([
20+
'name' => 'Technology',
21+
'handle' => 'technology',
22+
]);
23+
24+
$management = factory(\App\Department::class)->create([
25+
'name' => 'Management',
26+
'handle' => 'management',
27+
]);
28+
29+
$user = factory(\App\Role::class)->create([
30+
'department_id' => \App\Department::all()->random(),
31+
'name' => 'User',
32+
'handle' => 'user',
33+
]);
34+
35+
$staff = factory(\App\Role::class)->create([
36+
'department_id' => \App\Department::all()->random(),
37+
'name' => 'Staff',
38+
'handle' => 'staff',
39+
]);
40+
41+
$admin = factory(\App\Role::class)->create([
42+
'department_id' => \App\Department::all()->random(),
43+
'name' => 'Admin',
44+
'handle' => 'admin',
45+
]);
46+
47+
$users = factory(\App\User::class, 50)->create([
48+
'role_id' => \App\Role::all()->random(),
49+
]);
50+
51+
$users->each(function ($item, $key) {
52+
53+
$role = \App\Role::all()->random();
54+
55+
\DB::table('role_user')
56+
->insert([
57+
'role_id' => $role->id,
58+
'user_id' => $item->id,
59+
]);
60+
});
1561
}
1662
}

Diff for: local.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
"symlink": true
77
}
88
}
9-
],
9+
],
10+
11+
"jamesdordoy/laravelvuedatatable": "@dev",

Diff for: package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"devDependencies": {
1313
"axios": "^0.18.1",
14-
"bootstrap": "^4.0.0",
14+
"bootstrap": "^4.4.1",
1515
"cross-env": "^5.2.1",
1616
"jquery": "^3.2",
1717
"laravel-mix": "^4.1.4",

0 commit comments

Comments
 (0)