|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
|
4
|
|
|
use Illuminate\Database\Seeder; |
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
|
6
|
|
|
use Laravel\ProductFields\Models\Product; |
|
7
|
|
|
|
|
8
|
|
|
class ProductsSeeder extends Seeder |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Carbon |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $now; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $products = [ |
|
19
|
|
|
[ |
|
20
|
|
|
'id' => 1, |
|
21
|
|
|
'name' => 'iPhone 6 32GB', |
|
22
|
|
|
'fields' => [ |
|
23
|
|
|
1 => 1, // Operating System: iOS |
|
24
|
|
|
2 => 5, // RAM Size: 1GB |
|
25
|
|
|
3 => 8, // Storage Size: 32GB |
|
26
|
|
|
4 => 12, // Screen Resolution: 375x667 |
|
27
|
|
|
5 => 21, // CPU Cores Count: 2 |
|
28
|
|
|
], |
|
29
|
|
|
], |
|
30
|
|
|
[ |
|
31
|
|
|
'id' => 2, |
|
32
|
|
|
'name' => 'iPhone 7 32GB', |
|
33
|
|
|
'fields' => [ |
|
34
|
|
|
1 => 1, // Operating System: iOS |
|
35
|
|
|
2 => 6, // RAM Size: 1GB |
|
36
|
|
|
3 => 8, // Storage Size: 32GB |
|
37
|
|
|
4 => 18, // Screen Resolution: 375x667 |
|
38
|
|
|
5 => 21, // CPU Cores Count: 2 |
|
39
|
|
|
], |
|
40
|
|
|
], |
|
41
|
|
|
[ |
|
42
|
|
|
'id' => 3, |
|
43
|
|
|
'name' => 'Samsung Galaxy S7', |
|
44
|
|
|
'fields' => [ |
|
45
|
|
|
1 => 2, // Operating System: Android |
|
46
|
|
|
2 => 7, // RAM Size: 2GB |
|
47
|
|
|
3 => 8, // Storage Size: 32GB |
|
48
|
|
|
4 => 19, // Screen Resolution: 1920x1080 |
|
49
|
|
|
5 => 23, // CPU Cores Count: 6 |
|
50
|
|
|
], |
|
51
|
|
|
], |
|
52
|
|
|
[ |
|
53
|
|
|
'id' => 4, |
|
54
|
|
|
'name' => 'Samsung Galaxy S8', |
|
55
|
|
|
'fields' => [ |
|
56
|
|
|
1 => 2, // Operating System: Android |
|
57
|
|
|
2 => 7, // RAM Size: 2GB |
|
58
|
|
|
3 => 9, // Storage Size: 64GB |
|
59
|
|
|
4 => 19, // Screen Resolution: 1920x1080 |
|
60
|
|
|
5 => 24, // CPU Cores Count: 8 |
|
61
|
|
|
], |
|
62
|
|
|
], |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Run the products seeder. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function run() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->now = Carbon::now(); |
|
71
|
|
|
|
|
72
|
|
|
foreach ($this->products as $product) { |
|
73
|
|
|
DB::table('products')->insert($this->createProduct($product)); |
|
74
|
|
|
DB::table('fieldables')->insert($this->productFields($product['id'], $product['fields'])); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param array $product |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
View Code Duplication |
protected function createProduct(array $product) |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
return [ |
|
86
|
|
|
'id' => $product['id'], |
|
87
|
|
|
'name' => $product['name'], |
|
88
|
|
|
'created_at' => $this->now, |
|
89
|
|
|
'updated_at' => $this->now, |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param int $productId |
|
95
|
|
|
* @param array $fields |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function productFields(int $productId, array $fields) |
|
100
|
|
|
{ |
|
101
|
|
|
$productFields = []; |
|
102
|
|
|
|
|
103
|
|
|
foreach ($fields as $fieldId => $valueId) { |
|
104
|
|
|
$productFields[] = [ |
|
105
|
|
|
'field_id' => $fieldId, |
|
106
|
|
|
'field_value_id' => $valueId, |
|
107
|
|
|
'fieldable_type' => Product::class, |
|
108
|
|
|
'fieldable_id' => $productId, |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $productFields; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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.