コード例 #1
0
 public function addImageToUser($user)
 {
     $directory = base_path() . '/sample_images';
     $scanned_directory = array_diff(scandir($directory), array('..', '.'));
     $scanned_directory = array_values($scanned_directory);
     $random_file = base_path() . '/sample_images/' . $scanned_directory[rand(0, count($scanned_directory) - 1)];
     $ext = pathinfo($random_file, PATHINFO_EXTENSION);
     $filename = uniqid() . "." . $ext;
     $destination_file = public_path() . "/uploads/" . $filename;
     copy($random_file, $destination_file);
     // deactivate other photos
     $affected = \App\Models\UserPhoto::where('status', '=', 1)->where('user_id', $user->id)->update(array('status' => 0));
     $userPhoto = new \App\Models\UserPhoto();
     $userPhoto->user_id = $user->id;
     $userPhoto->filename = $filename;
     $userPhoto->save();
 }
コード例 #2
0
 public function removeMyPhoto()
 {
     $input = \Request::all();
     if (!isset($input['photo']) || !isset($input['photo']['id'])) {
         return;
     }
     $userPhoto = UserPhoto::where('user_id', \Auth::user()->id)->where('id', $input['photo']['id'])->where('status', 1)->first();
     if ($userPhoto) {
         $userPhoto->status = 0;
         $userPhoto->save();
     }
     return ['removed' => 'ok'];
 }