当前位置: 首页>>代码示例>>PHP>>正文


PHP Group::firstOrNew方法代码示例

本文整理汇总了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');
                 }
             }
         }
     }
 }
开发者ID:philippejadin,项目名称:Mobilizator,代码行数:48,代码来源:ImportActions.php


注:本文中的app\Group::firstOrNew方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。