public function store(Request $request) { if ($this->readOnly) { return $this->response->build(['error' => 'method_not_allowed'], SymfonyResponse::HTTP_METHOD_NOT_ALLOWED); } $data = $request->all(); // hook: beforeValidate() if ($hookResult = $this->beforeValidate($request, $data)) { return $hookResult; } // hook: beforeStoreValidate() if ($hookResult = $this->beforeStoreValidate($request, $data)) { return $hookResult; } $validator = Validator::make($data, $this->model->getValidationRules()); if ($validator->fails()) { return $this->response->build(['error' => 'unprocessable_entity', 'validation_errors' => $validator->errors()->all()], SymfonyResponse::HTTP_UNPROCESSABLE_ENTITY); } // hook: beforeInsert() if ($hookResult = $this->beforeInsert($request, $data)) { return $hookResult; } $inserted = $this->model->create($data); $entity = $this->model->with($this->ctx->with())->find($inserted->id); // hook: afterInsert() if ($hookResult = $this->afterInsert($request, $entity)) { return $hookResult; } return $this->response->build($entity, SymfonyResponse::HTTP_CREATED); }
/** * Create a new entity * * @param array $data * * @return \Illuminate\Database\Eloquent\Model */ public function create(array $data) { return $this->attempt(function () { return $this->model->create($data); }); }
public static function createUpdate(array $attributes) { if ($obj = static::myWhereExists($attributes)) { $obj->fill($attributes); $obj->save(); } else { return parent::create($attributes); } return $obj; }