UpdateRelationTo.php 1.4 KB

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