1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Seeder; |
4
|
|
|
use \Bantenprov\Sekolah\Models\Bantenprov\Sekolah\AdminSekolah; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Usage : |
8
|
|
|
* [1] $ composer dump-autoload -o |
9
|
|
|
* [2] $ php artisan db:seed --class=UserSeeder |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
class BantenprovUserSeeder extends Seeder |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
/* text color */ |
15
|
|
|
protected $RED ="\033[0;31m"; |
16
|
|
|
protected $CYAN ="\033[0;36m"; |
17
|
|
|
protected $YELLOW ="\033[1;33m"; |
18
|
|
|
protected $ORANGE ="\033[0;33m"; |
19
|
|
|
protected $PUR ="\033[0;35m"; |
20
|
|
|
protected $GRN ="\e[32m"; |
21
|
|
|
protected $WHI ="\e[37m"; |
22
|
|
|
protected $NC ="\033[0m"; |
23
|
|
|
|
24
|
|
|
/* File name */ |
25
|
|
|
/* location : /databse/seeds/file_name.csv */ |
26
|
|
|
protected $fileName = "BantenprovUserSeeder.csv"; |
27
|
|
|
|
28
|
|
|
/* text info : default (true) */ |
|
|
|
|
29
|
|
|
protected $textInfo = true; |
30
|
|
|
|
31
|
|
|
/* model class */ |
32
|
|
|
protected $model; |
33
|
|
|
|
34
|
|
|
/* __construct */ |
35
|
|
|
public function __construct(){ |
36
|
|
|
|
37
|
|
|
$this->model = new App\User; |
38
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Run the database seeds. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function run() |
47
|
|
|
{ |
48
|
|
|
$this->insertData(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/* function insert data */ |
52
|
|
|
protected function insertData() |
53
|
|
|
{ |
54
|
|
|
/* silahkan di rubah sesuai kebutuhan */ |
55
|
|
|
foreach($this->readCSV() as $data){ |
56
|
|
|
$password['p1'] = rand(1376123,999234999); |
|
|
|
|
57
|
|
|
$password['p2'] = rand(785482,9785482); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$admin_sekolah = $this->model->create([ |
60
|
|
|
//'id' => $data['id'], |
|
|
|
|
61
|
|
|
'name' => strtolower($data['name']), |
62
|
|
|
'email' => strtolower($data['email']), |
63
|
|
|
'password' => bcrypt($password['p1'].$password['p2']) |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
if($data['sekolah_id'] != 0){ |
67
|
|
|
// attach role to user |
68
|
|
|
$admin_sekolah->attachRole(5); |
69
|
|
|
|
70
|
|
|
// create admin sekolah |
71
|
|
|
|
72
|
|
|
AdminSekolah::create([ |
73
|
|
|
'sekolah_id' => $data['sekolah_id'], |
74
|
|
|
'admin_sekolah_id' => $admin_sekolah->id, |
75
|
|
|
'user_id' => '1', |
76
|
|
|
]); |
77
|
|
|
}elseif($data['sekolah_id'] == 0){ |
78
|
|
|
$admin_sekolah->attachRole(1); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if($this->textInfo){ |
82
|
|
|
echo "============[Account]============\n"; |
83
|
|
|
|
84
|
|
|
$this->orangeText('name : ').$this->greenText(strtolower($data['name'])); |
85
|
|
|
echo"\n"; |
86
|
|
|
$this->orangeText('email : ').$this->greenText(strtolower($data['email'])); |
87
|
|
|
echo"\n"; |
88
|
|
|
$this->orangeText('password : ').$this->greenText($password['p1'].$password['p2']); |
89
|
|
|
echo"\n"; |
90
|
|
|
echo "============[Account]============\n\n"; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
$this->greenText('[ SEEDER DONE ]'); |
97
|
|
|
echo"\n\n"; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/* text color: orange */ |
101
|
|
|
protected function orangeText($text) |
102
|
|
|
{ |
103
|
|
|
printf($this->ORANGE.$text.$this->NC); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/* text color: green */ |
107
|
|
|
protected function greenText($text) |
108
|
|
|
{ |
109
|
|
|
printf($this->GRN.$text.$this->NC); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/* function read CSV file */ |
113
|
|
|
protected function readCSV() |
114
|
|
|
{ |
115
|
|
|
|
116
|
|
|
$file = fopen(database_path("seeds/".$this->fileName), "r"); |
117
|
|
|
$all_data = array(); |
118
|
|
|
$row = 1; |
|
|
|
|
119
|
|
|
while(($data = fgetcsv($file, 1000, ",")) !== FALSE){ |
120
|
|
|
$all_data[] = ['name' => $data[0], 'email' => $data[1], 'sekolah_id' => $data[2]]; |
121
|
|
|
} |
122
|
|
|
fclose($file); |
123
|
|
|
|
124
|
|
|
return $all_data; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.