1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Seeder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Usage : |
7
|
|
|
* [1] $ composer dump-autoload -o |
8
|
|
|
* [2] $ php artisan db:seed --class=UserSeeder |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
class BantenprovUserSeederUser extends Seeder |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/* text color */ |
14
|
|
|
protected $RED ="\033[0;31m"; |
15
|
|
|
protected $CYAN ="\033[0;36m"; |
16
|
|
|
protected $YELLOW ="\033[1;33m"; |
17
|
|
|
protected $ORANGE ="\033[0;33m"; |
18
|
|
|
protected $PUR ="\033[0;35m"; |
19
|
|
|
protected $GRN ="\e[32m"; |
20
|
|
|
protected $WHI ="\e[37m"; |
21
|
|
|
protected $NC ="\033[0m"; |
22
|
|
|
|
23
|
|
|
/* File name */ |
24
|
|
|
/* location : /databse/seeds/file_name.csv */ |
25
|
|
|
protected $fileName = "BantenprovUserSeederUser.csv"; |
26
|
|
|
|
27
|
|
|
/* text info : default (true) */ |
|
|
|
|
28
|
|
|
protected $textInfo = true; |
29
|
|
|
|
30
|
|
|
/* model class */ |
31
|
|
|
protected $model; |
32
|
|
|
|
33
|
|
|
/* __construct */ |
34
|
|
|
public function __construct(){ |
35
|
|
|
|
36
|
|
|
$this->model = new App\User; |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Run the database seeds. |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function run() |
46
|
|
|
{ |
47
|
|
|
$this->insertData(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/* function insert data */ |
51
|
|
|
protected function insertData() |
52
|
|
|
{ |
53
|
|
|
/* silahkan di rubah sesuai kebutuhan */ |
54
|
|
|
foreach($this->readCSV() as $data){ |
55
|
|
|
$password['p1'] = rand(1376123,999234999); |
|
|
|
|
56
|
|
|
$password['p2'] = rand(785482,9785482); |
57
|
|
|
|
58
|
|
|
$this->model->create([ |
59
|
|
|
'name' => $data['name'], |
60
|
|
|
'email' => $data['email'], |
61
|
|
|
'password' => bcrypt($password['p1'].$password['p2']) |
62
|
|
|
]); |
63
|
|
|
|
64
|
|
|
if($this->textInfo){ |
65
|
|
|
echo "============[Account]============\n"; |
66
|
|
|
$this->orangeText('name : ').$this->greenText($data['name']); |
67
|
|
|
echo"\n"; |
68
|
|
|
$this->orangeText('email : ').$this->greenText($data['email']); |
69
|
|
|
echo"\n"; |
70
|
|
|
$this->orangeText('password : ').$this->greenText($password['p1'].$password['p2']); |
71
|
|
|
echo"\n"; |
72
|
|
|
echo "============[Account]============\n\n"; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->greenText('[ SEEDER DONE ]'); |
78
|
|
|
echo"\n\n"; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/* text color: orange */ |
82
|
|
|
protected function orangeText($text) |
83
|
|
|
{ |
84
|
|
|
printf($this->ORANGE.$text.$this->NC); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/* text color: green */ |
88
|
|
|
protected function greenText($text) |
89
|
|
|
{ |
90
|
|
|
printf($this->GRN.$text.$this->NC); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/* function read CSV file */ |
94
|
|
|
protected function readCSV() |
95
|
|
|
{ |
96
|
|
|
|
97
|
|
|
$file = fopen(database_path("seeds/".$this->fileName), "r"); |
98
|
|
|
$all_data = array(); |
99
|
|
|
$row = 1; |
|
|
|
|
100
|
|
|
while(($data = fgetcsv($file, 1000, ",")) !== FALSE){ |
101
|
|
|
$all_data[] = ['name' => $data[0], 'email' => $data[1]]; |
102
|
|
|
} |
103
|
|
|
fclose($file); |
104
|
|
|
|
105
|
|
|
return $all_data; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.