本文整理匯總了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));
});
}
}