コード例 #1
0
 /**
  * @return mixed
  */
 public function latest()
 {
     // Get all messages to and from the current user and sort them latest first
     $messages = Auth::user()->allChatMessages()->sortByDesc('created_at')->values();
     // Store the user id for later comparison
     $userId = Auth::user()->id;
     $uniqueChats = [];
     // Filter through the messages to find the first message in every conversation
     $filteredMessages = $messages->filter(function ($message) use($userId, &$uniqueChats) {
         if ($message->to_id != $userId && array_search($message->to_id, $uniqueChats) === false) {
             array_push($uniqueChats, $message->to_id);
             return true;
         } else {
             if ($message->from_id != $userId && array_search($message->from_id, $uniqueChats) === false) {
                 array_push($uniqueChats, $message->from_id);
                 return true;
             }
         }
     });
     // Select only the first ten results, this will keep the page nice and compact
     $selectedMessages = $filteredMessages->take(10);
     // Append partner information
     foreach ($selectedMessages as $message) {
         if ($message->to_id == $userId) {
             $message->user = User::find($message->from_id);
         } else {
             $message->user = User::find($message->to_id);
         }
     }
     return $selectedMessages;
 }
コード例 #2
0
 /**
  * Display a listing of the resource.
  *
  * @param \FashionDifferent\Element  $element
  * @param \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function index(Element $element, Request $request)
 {
     $comments = $element->comments->sortBy('id')->values();
     $filteredComments = null;
     $query = $request->query->all();
     if (count($query) && array_key_exists('since', $query)) {
         $since = intval($query['since']);
         $filteredComments = $comments->filter(function ($comment) use($since) {
             return $comment->id > $since;
         })->values();
     } else {
         $filteredComments = $comments;
     }
     foreach ($filteredComments as $comment) {
         $comment->user = User::find($comment->user_id);
         $comment->body = htmlspecialchars($comment->body);
     }
     return $filteredComments;
 }