/** * Store a newly created resource in storage. * * @param ResourceRequest $request * @return \Illuminate\Contracts\Http\Response */ public function store(ResourceRequest $request) { // Merging author_id. In real project // we should use $request->user()->id instead. $data = array_merge($request->all(), ['author_id' => 1]); if (!($resource = Resource::create($data))) { return $this->respond->internalError('Failed to create !'); } // respond created item with 201 status code return $this->respond->setStatusCode(201)->withItem($resource, new ResourceTransformer()); // respond with simple message return $this->respond->created('Created'); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return $this|\Illuminate\Contracts\Http\Response */ public function store(Request $request) { $this->validate($request, ['title' => 'required|min:2', 'description' => 'min:2']); // Merging author_id. In real project // we should use $request->user()->id instead. $data = array_merge($request->all(), ['author_id' => 1]); if (!($thing = Thing::create($data))) { return $this->respond->internalError('Failed to create !'); } // respond created item with 201 status code return $this->respond->setStatusCode(201)->withItem($thing, new ThingTransformer()); // respond with simple message //return $this->respond->created('Created'); }
/** * Store a newly created resource in storage. * * @param ThingsRequest $request * @return \Illuminate\Contracts\Http\Response */ public function store(ThingsRequest $request) { // Merging author_id. In real project // we should use $request->user()->id instead. $data = array_merge($request->all(), ['author_id' => 1]); if (!($thing = $this->model->create($data))) { return $this->respond->internalError('Failed to create !'); } // respond created item with 201 status code and location header return $this->respond->setStatusCode(201)->setHeaders(['Location' => route('v1.things.show', ['id' => $thing->id, 'include' => 'author'])])->withItem($thing, new ThingTransformer()); // respond simple message with 201 status code //return $this->respond->created('Created'); // You can also pass Eloquent model to the created method // Then it will append created resource to the response body //return $this->respond->created($thing); }