Esempio n. 1
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $user, $id)
 {
     $a = Auth::user();
     $data = Image::find($id);
     $user_id = $a->id;
     $auth = substr(md5($a->id . $a->email), 0, 10);
     $resource = $request->except(['_token', 'image', '_method']);
     if ($request->hasFile('image')) {
         $file = $request->file('image');
         $imageName = date('d-m-Y_h-i-s') . '_' . $user_id . '_' . $file->getClientOriginalName();
         $path = $this->path . '/' . $user_id . '_' . $auth;
         $destination = $path;
         $folder = public_path() . '/' . $path;
         if (!file_exists($folder)) {
             mkdir($folder);
         }
         $image = ['image_name' => $imageName, 'image_size' => $file->getSize(), 'fullsize_url' => $path . '/' . $imageName];
         if (unlink(public_path() . '/' . $data->fullsize_url) && $file->move($destination, $imageName) && Image::whereRaw("`id` = {$id} AND `user_id` = {$user_id}")->update(array_merge($resource, $image))) {
             return redirect()->route('photo.index', $user)->with(['message' => 'Your image has been update!']);
         }
     }
     if (Image::whereRaw("`id` = {$id} AND `user_id` = {$user_id}")->update($resource)) {
         return redirect()->route('photo.index', $user)->with(['message' => 'Your image has been update!']);
     }
     return redirect()->route('photo.edit', [$user, $id])->withErrors('Unexpected error!');
 }