本文整理汇总了PHP中Program::firstOrCreate方法的典型用法代码示例。如果您正苦于以下问题:PHP Program::firstOrCreate方法的具体用法?PHP Program::firstOrCreate怎么用?PHP Program::firstOrCreate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Program
的用法示例。
在下文中一共展示了Program::firstOrCreate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handle
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// 获取文本路径
$path = $this->argument('path');
if (!is_file($path)) {
return $this->error("Invalid file path {$path}.");
}
// 解析数据
$list = self::decodeText($path);
// 插入数据
foreach ($list as $group) {
DB::transaction(function () use(&$group) {
// 插入声音记录
self::insertAudios($group);
// 插入参与人记录
$data = $group['all'] ?? end($group);
$participantIds = [];
$participantNames = Participant::filterParticipantNames($data['participant']);
foreach ($participantNames as $name) {
$participant = Participant::firstOrCreate(['name' => $name]);
$participant->increment('counts', 1);
$participantIds[] = $participant->id;
}
// 插入节目记录
$topic = Program::filterTopic($data['topic']);
if (empty($group['all'])) {
if (in_array(mb_substr($topic, -1), ['a', 'b', 'c'])) {
$topic = mb_substr($topic, 0, -1);
}
}
$program = Program::firstOrCreate(['date' => $data['date'], 'topic' => $topic, 'state' => Program::STATE_ENABLE]);
if (!empty($participantIds)) {
$program->participants()->sync($participantIds);
}
// 输出日期
$this->info(sprintf("%s\t%s", $data['date'], $topic));
});
}
}