/**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     if (!Auth::check()) {
         Auth::logout();
         return redirect()->route('home');
     }
     $user = Auth::user();
     $first = Message::where("from", $user->id)->select("sender_img", "sender_name as s_navn", "sender", 'reader_name', 'id', 'from as my_id', 'from_name as my_name', 'message', 'to as sender_id', 'to_name as sender_name', 'created_at', 'read', 'reader');
     $all = Message::where("to", $user->id)->select("sender_img", "sender_name as s_navn", "sender", "reader_name", 'id', 'to as my_id', 'to_name as my_name', 'message', 'from as sender_id', 'from_name as sender_name', 'created_at', 'read', 'reader')->union($first)->orderBy('created_at', 'desc')->get();
     $books = Trend::all();
     $all_data = [];
     $count = 0;
     $sender_id = "";
     $id = "";
     $first_sender = "";
     // return $all;
     foreach ($all as $senders) {
         $temp = [];
         if ($count == 0 || $id != $senders->sender_id) {
             $sender_name = $senders->sender_name;
             $id = $senders->sender_id;
             if ($count == 0) {
                 $first_sender['name'] = str_replace(" ", "_", $sender_name);
                 $first_sender['id'] = $senders->sender_id;
             }
             $temp['from'] = $senders->my_id;
             $temp['from_name'] = $senders->my_name;
             $temp['message'] = $senders->message;
             $temp['sender_id'] = $senders->sender_id;
             $temp['sender_name'] = $senders->sender_name;
             $temp['created_at'] = $senders->created_at;
             $temp['read'] = $senders->read;
             $temp['reader'] = $senders->reader;
             $temp['id'] = $senders->id;
             $temp['r_navn'] = $senders->reader_name;
             $temp['s_id'] = $senders->sender;
             $temp['s_navn'] = $senders->s_navn;
             $temp['s_img'] = $senders->sender_img;
             $all_data[$sender_name][] = $temp;
         } else {
             $temp['from'] = $senders->my_id;
             $temp['from_name'] = $senders->my_name;
             $temp['message'] = $senders->message;
             $temp['sender_id'] = $senders->sender_id;
             $temp['sender_name'] = $senders->sender_name;
             $temp['created_at'] = $senders->created_at;
             $temp['read'] = $senders->read;
             $temp['reader'] = $senders->reader;
             $temp['id'] = $senders->id;
             $temp['r_navn'] = $senders->reader_name;
             $temp['s_id'] = $senders->sender;
             $temp['s_navn'] = $senders->s_navn;
             $temp['s_img'] = $senders->sender_img;
             $all_data[$sender_name][] = $temp;
         }
         $count++;
     }
     return view("profile.message", compact("all_data", "user", "books", "first_sender"));
 }
 /**
  * Show the application dashboard.
  *
  * @return Response
  */
 public function dashboard()
 {
     if (Auth::check()) {
         $my_books = Auth::user()->books->all();
         foreach ($my_books as $book_arr) {
             // get in stock and wishlist book
             if ($book_arr->is_lendable == '1') {
                 $in_stock[] = $book_arr;
             }
             if ($book_arr->is_wishlist == '1') {
                 $wish_list[] = $book_arr;
             }
         }
         // return $in_stock;
         $books = Trend::all();
         $title = "My Dashboard";
         $user = Auth::user();
         return view('home', compact("title", "books", "user", "in_stock", "wish_list"));
     } else {
         Auth::logout();
         return redirect()->route('home');
     }
 }
 public function bookNearYou()
 {
     if (Auth::check()) {
         $books = Trend::all();
         $user = Auth::user();
         $suggestions = [];
         $more_books = Book::all()->take(50);
         foreach ($more_books as $more) {
             if ($more->user_id != $user->id && $more->is_wishlist == '0') {
                 $matches = [];
                 $found_user = $more->user;
                 $theta = doubleval($found_user['longitude']) - doubleval($user->longitude);
                 $dist = sin(deg2rad(doubleval($found_user['latitude']))) * sin(deg2rad(doubleval($user->latitude))) + cos(deg2rad(doubleval($found_user['latitude']))) * cos(deg2rad(doubleval($user->latitude))) * cos(deg2rad($theta));
                 $dist = acos($dist);
                 $dist = rad2deg($dist);
                 $miles = round($dist * 60 * 1.1515 * 1.609344, 1);
                 $matches['location_name'] = $found_user['location_name'];
                 // $matches['firstname'] = $found_user['firstname'];
                 $matches['firstname'] = $found_user['name'];
                 $matches['distance'] = $miles;
                 $matches['title'] = $more->title;
                 $matches['image'] = $more->image;
                 $matches['rating'] = $more->rating;
                 $matches['publisher'] = $more->publisher;
                 $matches['isbn'] = $more->isbn;
                 $matches['id'] = $more->book_id;
                 $matches['desc'] = $more->desc;
                 // $matches['book_id'] = $more->
                 $suggestions[] = $matches;
             }
         }
         $suggestions = $this->sort_by_key($suggestions, 'distance');
         // $suggestions = array_slice($suggestions, 0, 50);
         return view("profile.nearby-books", compact("books", "suggestions", "user"));
     } else {
         Auth::logout();
         return redirect()->route('home');
     }
 }