1 | <?php |
||
2 | |||
3 | use Illuminate\Support\Facades\Schema; |
||
4 | use Illuminate\Database\Schema\Blueprint; |
||
5 | use Illuminate\Database\Migrations\Migration; |
||
6 | |||
7 | class SerializeConfig extends Migration |
||
0 ignored issues
–
show
|
|||
8 | { |
||
9 | /** |
||
10 | * Run the migrations. |
||
11 | * |
||
12 | * @return void |
||
13 | */ |
||
14 | public function up() |
||
15 | { |
||
16 | DB::table('config')->get()->each(function ($config) { |
||
17 | $value = $config->config_value; |
||
18 | |||
19 | if (filter_var($value, FILTER_VALIDATE_INT)) { |
||
20 | $value = (int)$value; |
||
21 | } elseif (filter_var($value, FILTER_VALIDATE_FLOAT)) { |
||
22 | $value = (float)$value; |
||
23 | } elseif (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null) { |
||
24 | $value = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
||
25 | } |
||
26 | |||
27 | DB::table('config') |
||
28 | ->where('config_id', $config->config_id) |
||
29 | ->update(['config_value' => json_encode($value)]); |
||
30 | }); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Reverse the migrations. |
||
35 | * |
||
36 | * @return void |
||
37 | */ |
||
38 | public function down() |
||
39 | { |
||
40 | DB::table('config')->get()->each(function ($config) { |
||
41 | $value = json_decode($config->config_value); |
||
42 | $value = is_bool($value) ? var_export($value, true) : (string)$value; |
||
43 | |||
44 | DB::table('config') |
||
45 | ->where('config_id', $config->config_id) |
||
46 | ->update(['config_value' => $value]); |
||
47 | }); |
||
48 | } |
||
49 | } |
||
50 |
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.