Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. <?php
  2.  
  3. use Illuminate\Support\Facades\Schema;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Database\Migrations\Migration;
  6.  
  7. class AddColumnsUpdatedCreated extends Migration
  8. {
  9. public function up()
  10. {
  11. foreach (self::getAllTables() as $tables__value) {
  12. Schema::table($tables__value, function (Blueprint $table) use ($tables__value) {
  13. if (!Schema::hasColumn($tables__value, 'created_by')) {
  14. $table->integer('created_by')->nullable();
  15. }
  16. if (!Schema::hasColumn($tables__value, 'updated_by')) {
  17. $table->integer('updated_by')->nullable();
  18. }
  19. if (!Schema::hasColumn($tables__value, 'created_at')) {
  20. $table->timestamp('created_at')->nullable();
  21. }
  22. if (!Schema::hasColumn($tables__value, 'updated_at')) {
  23. $table->timestamp('updated_at')->nullable();
  24. }
  25. });
  26. }
  27. }
  28.  
  29. public function down()
  30. {
  31. foreach (self::getAllTables() as $tables__value) {
  32. Schema::table($tables__value, function (Blueprint $table) use ($tables__value) {
  33. if (Schema::hasColumn($tables__value, 'created_by')) {
  34. $table->dropColumn('created_by');
  35. }
  36. if (Schema::hasColumn($tables__value, 'updated_by')) {
  37. $table->dropColumn('updated_by');
  38. }
  39. if (Schema::hasColumn($tables__value, 'created_at')) {
  40. $table->dropColumn('created_at');
  41. }
  42. if (Schema::hasColumn($tables__value, 'updated_at')) {
  43. $table->dropColumn('updated_at');
  44. }
  45. });
  46. }
  47. }
  48.  
  49. private function getAllTables()
  50. {
  51. $tables = [];
  52. foreach (
  53. DB::select(
  54. 'SELECT table_name FROM information_schema.tables WHERE table_catalog = ? AND table_type = ? AND table_schema = ? ORDER BY table_name',
  55. [env('DB_DATABASE'), 'BASE TABLE', 'public']
  56. )
  57. as $tables__value
  58. ) {
  59. $tables[] = $tables__value->table_name;
  60. }
  61. return $tables;
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement