1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Migrations\Migration; |
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
5
|
|
|
use Illuminate\Support\Facades\Schema; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
use Illuminate\Support\Facades\Log; |
8
|
|
|
|
9
|
|
|
class RemoveBackpackuserModel extends Migration |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Run the migrations. |
13
|
|
|
* |
14
|
|
|
* @return void |
15
|
|
|
*/ |
16
|
|
|
public function up() |
17
|
|
|
{ |
18
|
|
|
// establish the table names |
19
|
|
|
$model_has_roles = config('permission.table_names.model_has_roles'); |
20
|
|
|
$model_has_permissions = config('permission.table_names.model_has_permissions'); |
21
|
|
|
|
22
|
|
|
// replace the BackpackUser model with User |
23
|
|
|
$this->replaceModels($model_has_roles); |
24
|
|
|
$this->replaceModels($model_has_permissions); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function replaceModels($table_name) |
28
|
|
|
{ |
29
|
|
|
Log::info('Replacing BackpackUser model in '.$table_name); |
30
|
|
|
|
31
|
|
|
// if you've ended up with duplicate entries (both for App\User and App\Models\BackpackUser) |
32
|
|
|
// we can just delete them |
33
|
|
|
$userEntries = DB::table($table_name) |
34
|
|
|
->where("model_type", "App\User") |
35
|
|
|
->get(); |
36
|
|
|
|
37
|
|
|
foreach ($userEntries as $entry) { |
38
|
|
|
DB::table($table_name) |
39
|
|
|
->where('role_id', $entry->role_id) |
40
|
|
|
->where('model_type', 'App\Models\BackpackUser') |
41
|
|
|
->where('model_id', $entry->model_id) |
42
|
|
|
->delete(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// for the rest of them, we can just replace the BackpackUser model with User |
46
|
|
|
DB::table($table_name) |
47
|
|
|
->where("model_type", "App\Models\BackpackUser") |
48
|
|
|
->update([ |
49
|
|
|
"model_type" => "App\User" |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|