-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_check.php
More file actions
100 lines (76 loc) · 2.96 KB
/
simple_check.php
File metadata and controls
100 lines (76 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Simple Laravel Model Schema Checker
*/
require_once __DIR__ . '/vendor/autoload.php';
// Bootstrap Laravel
$app = require_once __DIR__ . '/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
echo "Laravel Model Schema Checker\n";
echo "============================\n\n";
// Configuration
$modelsPath = app_path('Models');
$excludedFields = ['id', 'created_at', 'updated_at', 'created_by', 'updated_by', 'deleted_by', 'deleted_at'];
// Find all model files
$modelFiles = File::allFiles($modelsPath);
$issuesFound = 0;
$modelsChecked = 0;
foreach ($modelFiles as $file) {
if ($file->getExtension() !== 'php') {
continue;
}
// Get class name
$className = 'App\\Models\\' . $file->getFilenameWithoutExtension();
if (!class_exists($className)) {
continue;
}
try {
$reflection = new ReflectionClass($className);
if (!$reflection->isSubclassOf('Illuminate\Database\Eloquent\Model')) {
continue;
}
$model = app($className);
$modelsChecked++;
echo "Checking model: {$className}\n";
$tableName = $model->getTable();
$fillable = $model->getFillable();
// Check if table exists
if (!DB::getSchemaBuilder()->hasTable($tableName)) {
echo " ❌ ERROR: Table '{$tableName}' does not exist\n";
$issuesFound++;
continue;
}
// Get table columns
$columns = DB::getSchemaBuilder()->getColumnListing($tableName);
$relevantColumns = array_diff($columns, $excludedFields);
// Check for fillable properties not in database
$extraFillable = array_diff($fillable, $columns);
if (!empty($extraFillable)) {
echo " ❌ ERROR: Fillable properties not in database: " . implode(', ', $extraFillable) . "\n";
$issuesFound++;
}
// Check for database columns not in fillable
$missingFillable = array_diff($relevantColumns, $fillable);
if (!empty($missingFillable)) {
echo " ⚠️ WARNING: Database columns not in fillable: " . implode(', ', $missingFillable) . "\n";
$issuesFound++;
}
if (empty($extraFillable) && empty($missingFillable)) {
echo " ✅ OK: Fillable properties match database schema\n";
}
echo "\n";
} catch (Exception $e) {
echo " ❌ ERROR: Could not check model {$className}: " . $e->getMessage() . "\n\n";
$issuesFound++;
}
}
echo "Summary:\n";
echo "========\n";
echo "Models checked: {$modelsChecked}\n";
echo "Issues found: {$issuesFound}\n";
if ($issuesFound > 0) {
echo "\n🔧 To fix fillable arrays automatically, you could run with --fix option (not implemented in this simple version)\n";
}