Created
May 11, 2017 19:53
-
-
Save mbdwey/abe5da3e126eb9d26ffc4b5202fd0024 to your computer and use it in GitHub Desktop.
voyager bread seeder
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
<?php | |
use Illuminate\Database\Seeder; | |
use TCG\Voyager\Models\DataRow; | |
use TCG\Voyager\Models\DataType; | |
use TCG\Voyager\Models\Menu; | |
use TCG\Voyager\Models\MenuItem; | |
use TCG\Voyager\Models\Permission; | |
use TCG\Voyager\Models\Role; | |
class ClassName extends Seeder | |
{ | |
/** | |
* Run the database seeds. | |
* | |
* @return void | |
*/ | |
public function run() | |
{ | |
$pulkAdd = [ | |
[ | |
'name' => 'slides','title' =>'Slides','model' => 'Slide','icon' => 'voyager-play','description' => '', | |
'columns' => [ | |
'title' => 'Title', | |
'description' => 'Description', | |
'image' => [ | |
'name' => 'Image', | |
'type' => 'file' | |
] | |
] | |
] | |
]; | |
foreach ($pulkAdd as $current) | |
{ | |
${$current['name']} = $this->dataType('slug', $current['name']); | |
if (!${$current['name']}->exists) | |
{ | |
${$current['name']}->fill([ | |
'name' => $current['name'], | |
'display_name_singular' => $current['model'], | |
'display_name_plural' => $current['title'], | |
'icon' => $current['icon'], | |
'model_name' => "App\\{$current['model']}", | |
'controller' => '', | |
'generate_permissions' => 1, | |
'description' => $current['description'], | |
])->save(); | |
foreach ($current['columns'] as $key => $value) | |
{ | |
if (is_array($value)) | |
{ | |
$display_name = $value['name']; | |
unset($value['name']); | |
$this->adddataRow(${$current['name']},$key,$display_name,$value); | |
} | |
else | |
{ | |
$this->adddataRow(${$current['name']},$key,$value); | |
} | |
} | |
Permission::generateFor($current['name']); | |
$this->MenuItem($current['title'],$current['name'],$current['icon']); | |
} | |
} | |
// sync new permissions with admin rold | |
$role = Role::where('name', 'admin')->firstOrFail(); | |
$permissions = Permission::all(); | |
$role->permissions()->sync( | |
$permissions->pluck('id')->all() | |
); | |
} | |
protected function MenuItem($title,$model,$icon = 'voyager-play') | |
{ | |
if (file_exists(base_path('routes/web.php'))) { | |
require base_path('routes/web.php'); | |
$menu = Menu::where('name', 'admin')->firstOrFail(); | |
$menuItem = MenuItem::firstOrNew([ | |
'menu_id' => $menu->id, | |
'title' => $title, | |
'url' => route("voyager.{$model}.index", [], false), | |
]); | |
if (!$menuItem->exists) { | |
$menuItem->fill([ | |
'target' => '_self', | |
'icon_class' => $icon, | |
'color' => null, | |
'parent_id' => null, | |
'order' => 6, | |
])->save(); | |
} | |
} | |
} | |
protected function adddataRow($parentModel,$key,$display_name,$overWrite = null) | |
{ | |
$rowDes = [ | |
'type' => 'text', | |
'required' => 1, | |
'browse' => 1, | |
'read' => 1, | |
'edit' => 1, | |
'add' => 1, | |
'delete' => 1, | |
'details' => '', | |
]; | |
if (isset($overWrite)) | |
{ | |
$rowDes = array_merge($rowDes,$overWrite); | |
} | |
$rowDes['display_name'] = $display_name; | |
$dataRow = $this->dataRow($parentModel, $key); | |
if (!$dataRow->exists) { | |
$dataRow->fill($rowDes)->save(); | |
} | |
} | |
protected function dataType($field, $for) | |
{ | |
return DataType::firstOrNew([$field => $for]); | |
} | |
protected function dataRow($type, $field) | |
{ | |
return DataRow::firstOrNew([ | |
'data_type_id' => $type->id, | |
'field' => $field, | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment