本文整理汇总了PHP中app\Group::firstOrNew方法的典型用法代码示例。如果您正苦于以下问题:PHP Group::firstOrNew方法的具体用法?PHP Group::firstOrNew怎么用?PHP Group::firstOrNew使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\Group
的用法示例。
在下文中一共展示了Group::firstOrNew方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handle
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// open csv file from storage
$csv = Reader::createFromPath(storage_path('import/actions.csv'));
//get the first row, usually the CSV header
$headers = $csv->fetchOne();
// validate headers
//get 25 rows starting from the 11th row
$res = $csv->setOffset(1)->fetchAll();
// build a nice associative array from the csv
foreach ($res as $data) {
foreach ($headers as $key => $header) {
$action[$header] = $data[$key];
}
$actions_data[] = $action;
}
foreach ($actions_data as $action_data) {
if (isset($action_data['id'])) {
$action = \App\Action::firstOrNew(['id' => $action_data['id']]);
$action->name = $action_data['name'];
$action->body = $action_data['body'];
$action->start = \Carbon\Carbon::parse($action_data['start']);
$action->stop = \Carbon\Carbon::parse($action_data['stop']);
$action->location = $action_data['location'];
$action->user_id = 1;
// create the group if not existing yet and associate it.
if (!empty($action_data['group_id']) && !empty($action_data['group'])) {
$group = \App\Group::firstOrNew(['id' => $action_data['group_id'] + $this->id_offset]);
$group->name = $action_data['group'];
$group->body = 'No description';
$group->save();
$action->group()->associate($group);
$this->info($group->name . ' GROUP has been created/updated in db');
$action->save();
if ($action->isInvalid()) {
$this->error($action->getErrors());
} else {
$this->info($action->name . ' ACTION has been created/updated in db');
}
}
}
}
}