sorting - How to sort merged collections in Laravel 5? -


i working on chat application have used pusher realtime chat, meanwhile storing every message in db too.

messages table schema looks so:

public function up() {     schema::create('messages', function(blueprint $table)     {         $table->increments('id');         $table->integer('sender_id')->unsigned();         $table->foreign('sender_id')->references('id')->on('users')->ondelete('cascade');         $table->integer('receiver_id')->unsigned();         $table->foreign('receiver_id')->references('id')->on('users')->ondelete('cascade');         $table->text('body');         $table->timestamps();     }); } 

it simple. 2 foreign keys refer sender , receiver.

the chat working fine , users can chat each other. need users can see older messages.

i have messagecontroller in index method looks so:

public function index() {     $data = $this->request->all();      $receivedmessages = $this->user->currentuser()->receivedmessages()                 ->where('sender_id', $data['chatuserid'])                 ->get();      $sentmessages = $this->user->currentuser()->sentmessages()                 ->where('receiver_id', $data['chatuserid'])                 ->get();      $messages = $receivedmessages->merge($sentmessages)->sortby('created_at');      return $messages; } 

basically fetching messages current user either sender or receiver , vice versa the other user, id of supply front end in chatuserid.

i have used merge method merge these collection , trying run sortby or sortbydesc methods, don't seem working.

i have simple dummy chat couple of users follows:

simple chat

the web service @ messagecontroller@index returns this:

{    "0":{       "id":"10",       "sender_id":"3",       "receiver_id":"1",       "body":"hi rohan. how you?",       "created_at":"2015-06-19 08:32:49",       "updated_at":"2015-06-19 08:32:49"    },    "1":{       "id":"12",       "sender_id":"3",       "receiver_id":"1",       "body":"i have been too.",       "created_at":"2015-06-19 08:33:06",       "updated_at":"2015-06-19 08:33:06"    },    "2":{       "id":"9",       "sender_id":"1",       "receiver_id":"3",       "body":"hi ava",       "created_at":"2015-06-19 08:32:41",       "updated_at":"2015-06-19 08:32:41"    },    "3":{       "id":"11",       "sender_id":"1",       "receiver_id":"3",       "body":"i ava. how have been?",       "created_at":"2015-06-19 08:32:59",       "updated_at":"2015-06-19 08:32:59"    } } 

this not sorted way want be. how sort merged collections in laravel created_at field?

if don't need sent , received messages in 2 differents collections, can like:

$messages = messages::where(function ($query) {                           $query->where('sender_id', user_id)                           ->where('receiver_id', $data['chatuserid']);                       })                       ->orwhere(function ($query) {                           $query->where('sender_id', $data['chatuserid'])                           ->where('receiver_id', user_id);                       })                       ->sortby('created_at)                       ->get(); 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -