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 BantenprovNilaiSeeder 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 = "BantenprovNilaiSeeder.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 Bantenprov\Nilai\Models\Bantenprov\Nilai\Nilai; |
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
|
|
|
|
56
|
|
|
$this->model->create([ |
|
|
|
|
57
|
|
|
'user_id' => $data['user_id'], |
58
|
|
|
'nomor_un' => $data['nomor_un'], |
59
|
|
|
'siswa_id' => $data['siswa_id'], |
60
|
|
|
'akademik_id' => $data['akademik_id'], |
61
|
|
|
'prestasi_id' => $data['prestasi_id'], |
62
|
|
|
'zona_id' => $data['zona_id'], |
63
|
|
|
'sktm_id' => $data['sktm_id'], |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
if($this->textInfo){ |
67
|
|
|
echo "============[DATA]============\n"; |
68
|
|
|
$this->orangeText('user_id : ').$this->greenText($data['user_id']); |
69
|
|
|
echo"\n"; |
70
|
|
|
$this->orangeText('nomor_un : ').$this->greenText($data['nomor_un']); |
71
|
|
|
echo"\n"; |
72
|
|
|
$this->orangeText('siswa_id : ').$this->greenText($data['siswa_id']); |
73
|
|
|
echo"\n"; |
74
|
|
|
$this->orangeText('akademik_id : ').$this->greenText($data['akademik_id']); |
75
|
|
|
echo"\n"; |
76
|
|
|
$this->orangeText('prestasi_id : ').$this->greenText($data['prestasi_id']); |
77
|
|
|
echo"\n"; |
78
|
|
|
$this->orangeText('zona_id : ').$this->greenText($data['zona_id']); |
79
|
|
|
echo"\n"; |
80
|
|
|
$this->orangeText('sktm_id : ').$this->greenText($data['sktm_id']); |
81
|
|
|
echo"\n"; |
82
|
|
|
echo "============[DATA]============\n\n"; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->greenText('[ SEEDER DONE ]'); |
88
|
|
|
echo"\n\n"; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/* text color: orange */ |
92
|
|
|
protected function orangeText($text) |
93
|
|
|
{ |
94
|
|
|
printf($this->ORANGE.$text.$this->NC); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/* text color: green */ |
98
|
|
|
protected function greenText($text) |
99
|
|
|
{ |
100
|
|
|
printf($this->GRN.$text.$this->NC); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/* function read CSV file */ |
104
|
|
|
protected function readCSV() |
105
|
|
|
{ |
106
|
|
|
/* Silahkan di rubah sesuai struktur file csv */ |
107
|
|
|
$file = fopen(database_path("seeds/".$this->fileName), "r"); |
108
|
|
|
$all_data = array(); |
109
|
|
|
$row = 1; |
|
|
|
|
110
|
|
|
while(($data = fgetcsv($file, 1000, ",")) !== FALSE){ |
111
|
|
|
$all_data[] = ['user_id' => $data[0], |
112
|
|
|
'nomor_un' => $data[1], |
113
|
|
|
'siswa_id' => $data[2], |
114
|
|
|
'akademik_id' => $data[3], |
115
|
|
|
'prestasi_id' => $data[4], |
116
|
|
|
'zona_id' => $data[5], |
117
|
|
|
'sktm_id' => $data[6], |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
fclose($file); |
121
|
|
|
|
122
|
|
|
return $all_data; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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.