Создание консольной команды artisan
Пытаюсь создать команду с помощью этого мануала https://www.digitalocean.com/community/tutorials/how-to-create-artisan-commands-to-manage-database-records-in-laravel. По нему все работает хорошо. Я ввожу созданную команду, ввожу ссылку, описание и все это сохраняется в базу.
Сейчас я работаю с redmine api. Мне нужно получить оттуда трудозатраты и с помощью созданной консольной команды загрузить данные из redmine в базу.
Но не совсем понимаю как это сделать. Вот мой код:
Model Time Entry
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $project
* @property int $issue
* @property string $activity
* @property string $comments
* @property int $hours
* @property int $month
* @property int $year
*/
class TimeEntry extends Model
{
use HasFactory;
protected $guarded = [
'id',
];
}
Commands/LinkNew (Здесь я получаю исключение Illuminate\Database\Query\Grammars\Grammar::Illuminate\Database\Query\Grammars\{closure}("2022-08-21 08:26:13", "updated_at"))
<?php
namespace App\Console\Commands;
use App\Models\TimeEntry;
use App\Services\RedmineAPIService;
use Illuminate\Console\Command;
class LinkNew extends Command
{
protected $signature = 'link:new';
protected $description = 'Create a New Link';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$redmineService = new RedmineAPIService();
$users = $redmineService->getUsersInfo();
//isset($user);
foreach ($users as $user) {
$id = $redmineService->getTimeEntriesInfo($user['id']);
$project = $redmineService->getTimeEntriesInfo($user['id']);
$activity = $redmineService->getTimeEntriesInfo($user['id']);
$issue = $redmineService->getTimeEntriesInfo($user['id']);
$comments = $redmineService->getTimeEntriesInfo($user['id']);
if ($this->confirm('Is this information correct?')) {
$link = new TimeEntry();
$link->id = $id;
$link->project = $project;
$link->activity = $activity;
$link->issue = $issue;
$link->comments = $comments;
$link->save();
$this->info("Saved.");
}
}
return 0;
}
}
RedmineAPIService
public function getTimeEntriesInfo(int $user_id): array
{
$users = self::getAllData('time_entry', ['user_id'=>$user_id]);
return $users['time_entries'];
}
И вот вопрос, как расписать функцию handle(). В моем примере данные id, project, activity, issue, comments я получаю с помощью функции getTimeEntriesInfo(), которая расписана в RedmineAPIService.
UPD:
public function handle()
{
$redmineService = new RedmineAPIService();
$users = $redmineService->getUsersInfo();
foreach ($users as $user) {
$data = $redmineService->getTimeEntriesInfo($user['id']);
$id = $data['id'];
$project = $data['id'];
$activity = $data['id'];
$issue = $data['id'];
$comments = $data['id'];
if ($this->confirm('Is this information correct?')) {
$link = new TimeEntry();
$link->id = $id;
$link->project = $project;
$link->activity = $activity;
$link->issue = $issue;
$link->comments = $comments;
// dd($link->getAttributes());
$link->save();
$this->info("Saved.");
}
}
return 0;
}
dd($link->getAttributes());
249 => array:10 [
"id" => 5369
"project" => array:2 [ …2]
"issue" => array:1 [ …1]
"user" => array:2 [ …2]
"activity" => array:2 [ …2]
"hours" => 1.0
"comments" => "Комментарий"
"spent_on" => "2022-05-27"
"created_on" => "2022-05-27T05:00:19Z"
"updated_on" => "2022-05-27T05:00:19Z"
]