Guest User

Untitled

a guest
Jan 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use Illuminate\Console\Command;
  6.  
  7. use \App\Models\Catalogue\Item;
  8.  
  9. class UpdateItemsProperties extends Command
  10. {
  11.  
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'update:items:properties';
  18.  
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Get items properties from description table, make it with many-to-many relation and clean it';
  25.  
  26.  
  27. private $names = [
  28. [
  29. 'from' => [
  30. 'Стандарты Wi-Fi', // названия свойств, которые будут изменены на %to%
  31. ],
  32. 'to' => 'Стандарт Wi-Fi', // на что обновить найденные свойства
  33. ],
  34. ];
  35.  
  36.  
  37.  
  38. /**
  39. * Create a new command instance.
  40. *
  41. * @return void
  42. */
  43. public function __construct()
  44. {
  45. parent::__construct();
  46. }
  47.  
  48. /**
  49. * Execute the console command.
  50. *
  51. * @return mixed
  52. */
  53. public function handle()
  54. {
  55.  
  56. $items = Item::all();
  57.  
  58. foreach ($items as $item) {
  59. $item->updateProperties();
  60. }
  61.  
  62. $this->updateRepeatingProperties();
  63.  
  64. }
  65.  
  66.  
  67. /**
  68. * Удаляет свойства, которые повторяются с разными названиями
  69. **/
  70. public function updateRepeatingProperties()
  71. {
  72. foreach ($this->names as $name)
  73. {
  74. foreach ($name['from'] as $from)
  75. {
  76. if ($oldProperty = \App\Models\Catalogue\Property::where('title', $from)->with('items')->first())
  77. {
  78. if (!empty($name['to'])) // если null, то просто удалить старое свойство
  79. {
  80. $newProperty = \App\Models\Catalogue\Property::firstOrCreate([
  81. 'title' => $name['to'],
  82. 'slug' => str_slug($name['to']),
  83. ]);
  84.  
  85. $newProperty->save();
  86.  
  87. foreach ($oldProperty->items as $item) {
  88.  
  89. $newProperty->items()->attach($item->id, [
  90. 'value_string' => $item->pivot->value_string,
  91. 'value_integer' => $item->pivot->value_integer,
  92. ]);
  93.  
  94. echo "Установлено свойство для {$item->prod} {$item->art}: \n ";
  95. echo "{$newProperty->title}: {$item->pivot->value_string} / {$item->pivot->value_integer} \n";
  96. echo "______________________________________________ \n";
  97.  
  98. }
  99. }
  100. echo "Свойство \"{$oldProperty->title}\" будет удалено \n";
  101.  
  102. $oldProperty->delete();
  103. }
  104. }
  105. }
  106. }
  107. }
Add Comment
Please, Sign In to add comment