Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
 /**
  * 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);
     });
 }
Ejemplo n.º 3
0
 public static function createUpdate(array $attributes)
 {
     if ($obj = static::myWhereExists($attributes)) {
         $obj->fill($attributes);
         $obj->save();
     } else {
         return parent::create($attributes);
     }
     return $obj;
 }