Example #1
0
 /**
  * Create and attach a new command to this action
  *
  * @param string $command Command to add
  *
  * @return \App\Models\Command
  */
 public function addCommand($command)
 {
     $commandModel = new Command();
     $commandModel->command = $command;
     $commandModel->save();
     $this->commands()->save($commandModel);
 }
 /**
  * Transforms the given data into the related model
  *
  * @param stdClass $data Data to transform
  *
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function transform($data)
 {
     $action = parent::transform($data);
     if (isset($data->host)) {
         $hostData = $data->host;
         $host = new Host();
         if (isset($hostData->id)) {
             try {
                 $host = $host->findOrFail($hostData->id);
             } catch (ModelNotFoundException $exeception) {
             }
             // Only set host properties if this is a new host.
             // Don't want to be overriding existing host data, just in case.
         } else {
             if (isset($hostData->attributes)) {
                 $host->fill($hostData->attributes);
             }
         }
         $action->host()->associate($host);
         $action->save();
     }
     $commands = $data->commands;
     $commandModels = [];
     foreach ($commands as $command) {
         $commandModel = new Command();
         $commandModel->command = $command;
         $commandModel->save();
         $commandModels[] = $commandModel;
     }
     $action->commands()->saveMany($commandModels);
     return $action;
 }