/** * Creates a type * * @param null|array $data * @return null|TypeModel */ public function create($data = null) { DB::beginTransaction(); try { if (is_null($data)) { throw new NullDataException('The data array is required!'); } foreach ($data as $k => $v) { if (!in_array($k, $this->fields)) { throw new CreateTypeException('Please add the correct fields!'); } } if (count(array_keys($data)) != count($this->fields)) { throw new CreateTypeException('The number of given data is different than required!'); } if (TypeModel::whereName($data['name'])->first()) { throw new CreateTypeException('The type name already exists!'); } $type = TypeModel::create($data); DB::commit(); return $type; } catch (NullDataException $e) { DB::rollBack(); return $e; } catch (CreateTypeException $e) { DB::rollBack(); return $e; } }
public function store(CreateTypeRequest $request) { $activity = TypeModel::create($request->all()); return TypeModel::whereId($activity->id)->first()->toJson(); }