/** * Update the specified resource in storage. * PATCH /comments/{id} * * @param int $id * @return Response */ public function update($id) { // $validator = Comment::validate(Input::all()); $user = Auth::user(); // if ($validator->fails()) // return $this->respondInsufficientPrivileges($validator->messages()->all()); if (Input::get('text') == '' & !Input::has('attachments')) { return $this->respondInsufficientPrivileges('Send some text'); } if (strlen(Input::get('text')) > 2500) { return $this->respondInsufficientPrivileges('Слишком длинный текст'); } $comment = Comment::find($id); if (!$comment) { return $this->respondNotFound('Comment not found'); } if (!$user->can('Comment.update', $comment)) { return $this->respondInsufficientPrivileges('Unauthorized action'); } $comment->fill(Input::all()); if ($comment->save()) { if (Input::has('attachments') && !empty(Input::get('attachments'))) { $attachments = Input::get('attachments'); $comment->cars()->detach(); $comment->geos()->detach(); $comment->carsWithNumbers()->detach(); foreach ($attachments as $attachment) { $carHelper = new Helpers\carHelper(); if ($attachment['type'] == 'Geo') { $geo = Geo::create(['long' => $attachment['long'], 'lat' => $attachment['lat'], 'location' => $attachment['location']]); $comment->geos()->save($geo); } if ($attachment['type'] == 'Car') { $car = $carHelper::fetchCar($user, $attachment['id']); if ($car) { $comment->cars()->attach($car->id); } } if ($attachment['type'] == 'CarNumber') { $car = $carHelper::fetchCar($user, $attachment['id']); if ($car) { $comment->carsWithNumbers()->attach($car->id); } } if ($attachment['type'] == 'Image') { $image = Image::find($attachment['id']); if ($image && !$comment->images()->find($attachment['id'])) { $comment->images()->save($image); } if ($comment->images()->find($attachment['id'])) { $images[] = $image->id; } } } if (isset($images)) { $comment->images()->whereNotIn('id', $images)->delete(); } else { $comment->images()->delete(); } } else { $comment->images()->delete(); $comment->cars()->detach(); $comment->geos()->detach(); $comment->carsWithNumbers()->detach(); } return $this->respond($this->collectionTransformer->transformComment($comment)); } return $this->respondServerError(); }
public function adminUpdate($id) { // dd(Input::all()); $post = Post::find($id); if (!$post) { App::abort(404); } $post->fill(Input::all()); if (Input::has('geo')) { $post->geos()->detach(); $post->geos()->save(Geo::create(['long' => Input::get('geo')['long'], 'lat' => Input::get('geo')['lat'], 'location' => Input::get('geo')['location']])); } if (Input::has('car')) { $post->cars()->detach(); $user = User::find($post->user_id); $carHistory = $user->carsHistory()->create(['mark' => Input::get('car')['mark'] != 0 ?: null, 'model' => Input::get('car')['model'] != 0 ?: null, 'year' => Input::get('car')['year'] != 0 ?: null, 'color' => Input::get('car')['color'] != 0 ?: null, 'body_type' => Input::get('car')['body_type'] != 0 ?: null]); $post->cars()->attach($carHistory->id); } if (Input::has('carNumber')) { $post->carsWithNumbers()->detach(); $user = User::find($post->user_id); $carNumber = $user->carsHistory()->create(['mark' => Input::get('carNumber')['mark'] != 0 ?: null, 'model' => Input::get('carNumber')['model'] != 0 ?: null, 'year' => Input::get('carNumber')['year'] != 0 ?: null, 'color' => Input::get('carNumber')['color'] != 0 ?: null, 'body_type' => Input::get('carNumber')['body_type'] != 0 ?: null]); $post->carsWithNumbers()->attach($carNumber->id); } if (Input::has('images')) { foreach (Input::get('images') as $image) { dd($image); } } if ($post->save()) { return Redirect::to('/admin/posts'); } App::abort(500); }