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