1
|
|
|
<?php |
2
|
|
|
use Illuminate\Database\Seeder; |
3
|
|
|
/** |
4
|
|
|
* Usage : |
5
|
|
|
* [1] $ composer dump-autoload -o |
6
|
|
|
* [2] $ php artisan db:seed --class=BantenprovPrestasiSeederPrestasi |
7
|
|
|
*/ |
8
|
|
|
class BantenprovPrestasiSeederPrestasi 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 = "BantenprovPrestasiSeederPrestasi.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\Prestasi\Models\Bantenprov\Prestasi\Prestasi; |
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
|
|
View Code Duplication |
foreach($this->readCSV() as $data){ |
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
$this->model->create([ |
47
|
|
|
'nomor_un' => $data['nomor_un'], |
48
|
|
|
'master_prestasi_id' => $data['master_prestasi_id'], |
49
|
|
|
'nama_lomba' => $data['nama_lomba'], |
50
|
|
|
'nilai' => $data['nilai'], |
51
|
|
|
'user_id' => $data['user_id'], |
52
|
|
|
|
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if($this->textInfo){ |
59
|
|
|
echo "============[DATA]============\n"; |
60
|
|
|
$this->orangeText('nomor_un : ').$this->greenText($data['nomor_un']); |
|
|
|
|
61
|
|
|
echo"\n"; |
62
|
|
|
$this->orangeText('master_prestasi_id : ').$this->greenText($data['master_prestasi_id']); |
63
|
|
|
echo"\n"; |
64
|
|
|
$this->orangeText('nama_lomba : ').$this->greenText($data['nama_lomba']); |
65
|
|
|
echo"\n"; |
66
|
|
|
$this->orangeText('nilai : ').$this->greenText($data['nilai']); |
67
|
|
|
echo"\n"; |
68
|
|
|
$this->orangeText('user_id : ').$this->greenText($data['user_id']); |
69
|
|
|
echo"\n"; |
70
|
|
|
|
71
|
|
|
echo "============[DATA]============\n\n"; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->greenText('[ SEEDER DONE ]'); |
75
|
|
|
echo"\n\n"; |
76
|
|
|
} |
77
|
|
|
/* text color: orange */ |
78
|
|
|
protected function orangeText($text) |
79
|
|
|
{ |
80
|
|
|
printf($this->ORANGE.$text.$this->NC); |
81
|
|
|
} |
82
|
|
|
/* text color: green */ |
83
|
|
|
protected function greenText($text) |
84
|
|
|
{ |
85
|
|
|
printf($this->GRN.$text.$this->NC); |
86
|
|
|
} |
87
|
|
|
/* function read CSV file */ |
88
|
|
View Code Duplication |
protected function readCSV() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$file = fopen(database_path("seeds/".$this->fileName), "r"); |
91
|
|
|
$all_data = array(); |
92
|
|
|
$row = 1; |
|
|
|
|
93
|
|
|
while(($data = fgetcsv($file, 1000, ",")) !== FALSE){ |
94
|
|
|
$all_data[] = [ |
95
|
|
|
'nomor_un' => $data[0], |
96
|
|
|
'master_prestasi_id' => $data[1], |
97
|
|
|
'nama_lomba' => $data[2], |
98
|
|
|
'nilai' => $data[3], |
99
|
|
|
'user_id' => $data[4], |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
fclose($file); |
103
|
|
|
return $all_data; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.