Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use \App\Models\Catalogue\Item;
- class UpdateItemsProperties extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'update:items:properties';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Get items properties from description table, make it with many-to-many relation and clean it';
- private $names = [
- [
- 'from' => [
- 'Стандарты Wi-Fi', // названия свойств, которые будут изменены на %to%
- ],
- 'to' => 'Стандарт Wi-Fi', // на что обновить найденные свойства
- ],
- ];
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $items = Item::all();
- foreach ($items as $item) {
- $item->updateProperties();
- }
- $this->updateRepeatingProperties();
- }
- /**
- * Удаляет свойства, которые повторяются с разными названиями
- **/
- public function updateRepeatingProperties()
- {
- foreach ($this->names as $name)
- {
- foreach ($name['from'] as $from)
- {
- if ($oldProperty = \App\Models\Catalogue\Property::where('title', $from)->with('items')->first())
- {
- if (!empty($name['to'])) // если null, то просто удалить старое свойство
- {
- $newProperty = \App\Models\Catalogue\Property::firstOrCreate([
- 'title' => $name['to'],
- 'slug' => str_slug($name['to']),
- ]);
- $newProperty->save();
- foreach ($oldProperty->items as $item) {
- $newProperty->items()->attach($item->id, [
- 'value_string' => $item->pivot->value_string,
- 'value_integer' => $item->pivot->value_integer,
- ]);
- echo "Установлено свойство для {$item->prod} {$item->art}: \n ";
- echo "{$newProperty->title}: {$item->pivot->value_string} / {$item->pivot->value_integer} \n";
- echo "______________________________________________ \n";
- }
- }
- echo "Свойство \"{$oldProperty->title}\" будет удалено \n";
- $oldProperty->delete();
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment