1
|
|
|
<?php |
2
|
|
|
use Illuminate\Database\Seeder; |
3
|
|
|
/** |
4
|
|
|
* Usage : |
5
|
|
|
* [1] $ composer dump-autoload -o |
6
|
|
|
* [2] $ php artisan db:seed --class=BantenprovJuara |
7
|
|
|
*/ |
8
|
|
View Code Duplication |
class BantenprovJuara 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 = "BantenprovJuara.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\Juara; |
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
|
|
|
'juara' => $data['juara'], |
48
|
|
|
|
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if($this->textInfo){ |
55
|
|
|
echo "============[DATA]============\n"; |
56
|
|
|
$this->orangeText('juara : ').$this->greenText($data['juara']); |
|
|
|
|
57
|
|
|
echo"\n"; |
58
|
|
|
|
59
|
|
|
echo "============[DATA]============\n\n"; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->greenText('[ SEEDER DONE ]'); |
63
|
|
|
echo"\n\n"; |
64
|
|
|
} |
65
|
|
|
/* text color: orange */ |
66
|
|
|
protected function orangeText($text) |
67
|
|
|
{ |
68
|
|
|
printf($this->ORANGE.$text.$this->NC); |
69
|
|
|
} |
70
|
|
|
/* text color: green */ |
71
|
|
|
protected function greenText($text) |
72
|
|
|
{ |
73
|
|
|
printf($this->GRN.$text.$this->NC); |
74
|
|
|
} |
75
|
|
|
/* function read CSV file */ |
76
|
|
|
protected function readCSV() |
77
|
|
|
{ |
78
|
|
|
$file = fopen(database_path("seeds/".$this->fileName), "r"); |
79
|
|
|
$all_data = array(); |
80
|
|
|
$row = 1; |
|
|
|
|
81
|
|
|
while(($data = fgetcsv($file, 1000, ",")) !== FALSE){ |
82
|
|
|
$all_data[] = [ |
83
|
|
|
'juara' => $data[0] |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
fclose($file); |
87
|
|
|
return $all_data; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.