2
0

UpdateRelationTo.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Relation;
  4. use App\Tools\Tools;
  5. use Illuminate\Console\Command;
  6. class UpdateRelationTo extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'update:relation.to';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. if (Tools::isStop()) {
  37. return 0;
  38. }
  39. $count = 0;
  40. $all = 0;
  41. foreach (Relation::select(['id', 'to'])->cursor() as $relation) {
  42. $all++;
  43. if (! empty($relation->to)) {
  44. $old = json_decode($relation->to, true);
  45. if (count(array_filter(array_keys($old), 'is_string')) === 0) {
  46. // 索引数组,需要转换
  47. $new = ['case' => $old];
  48. Relation::where('id', $relation->id)->update(['to' => json_encode($new)]);
  49. $count++;
  50. }
  51. }
  52. }
  53. $this->info("{$count} of {$all}");
  54. return 0;
  55. }
  56. }