-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from Laravel-Backpack/4.1
Added support for Backpack 4.1
- Loading branch information
Showing
6 changed files
with
74 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/database/migrations/2020_03_31_114745_remove_backpackuser_model.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class RemoveBackpackuserModel extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
// establish the table names | ||
$model_has_roles = config('permission.table_names.model_has_roles'); | ||
$model_has_permissions = config('permission.table_names.model_has_permissions'); | ||
|
||
// replace the BackpackUser model with User | ||
if (\Illuminate\Support\Facades\Schema::hasTable($model_has_roles)) { | ||
$this->replaceModels($model_has_roles); | ||
} | ||
if (\Illuminate\Support\Facades\Schema::hasTable($model_has_permissions)) { | ||
$this->replaceModels($model_has_permissions); | ||
} | ||
} | ||
|
||
public function replaceModels($table_name) | ||
{ | ||
Log::info('Replacing BackpackUser model in '.$table_name); | ||
|
||
// if you've ended up with duplicate entries (both for App\User and App\Models\BackpackUser) | ||
// we can just delete them | ||
$userEntries = DB::table($table_name) | ||
->where('model_type', "App\User") | ||
->get(); | ||
|
||
foreach ($userEntries as $entry) { | ||
DB::table($table_name) | ||
->where('role_id', $entry->role_id) | ||
->where('model_type', 'App\Models\BackpackUser') | ||
->where('model_id', $entry->model_id) | ||
->delete(); | ||
} | ||
|
||
// for the rest of them, we can just replace the BackpackUser model with User | ||
DB::table($table_name) | ||
->where('model_type', "App\Models\BackpackUser") | ||
->update([ | ||
'model_type' => "App\User", | ||
]); | ||
} | ||
} |