Archived
1
0
Fork 0
This repository has been archived on 2026-06-16. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
wow-raid-bingo/app/Console/Commands/CardImport.php
2021-10-18 11:56:52 +02:00

74 lines
1.6 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Character;
use App\Models\Raid;
use App\Models\Card;
use Illuminate\Support\Str;
class CardImport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'card:import {file}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import card data';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$file = $this->argument('file');
$data = file_get_contents($file);
try {
$json = json_decode($data, false, 8, JSON_THROW_ON_ERROR);
} catch(\JsonException $ex) {
$this->error("Json: " . $ex->getMessage());
return;
}
foreach($json as $item) {
$data = [
'body' => $item->message,
];
if (isset($item->character)) {
$character = Character::firstOrCreate(['name' => $item->character]);
$data['character_id'] = $character->id;
}
if (isset($item->raid)) {
$raid = Raid::firstOrCreate(['name' => $item->raid]);
$data['raid_id'] = $raid->id;
}
if (isset($item->class)) {
$data['class'] = Str::lower($item->class);
}
if (isset($item->role)) {
$data['role'] = $item->role;
}
Card::insert($data);
}
}
}