dimanche 9 décembre 2018

How to return receiver and sender objects from User model

I'm building an app that has a section for user to user chatting. Naturally, I have a User model for the users table and a Chat model for the messages. In the Chat model, I have a belongsTo relationship with the User model and a corresponding hasMany relationship in the User model. The chats table have sender_id, receiver_id & message columns. I'm fine up till here. However, in the controller, when chat is saved in the table, I'm trying to return a response of the chat including the sender and receiver objects using the relationship I created in the Chat model but keeps getting only the sender object. I also tried using an accessor on the chat model like so:

public function getReceiverAttribute(){ $receiver = User::find($this->receiver_id)->first(); return $receiver; }

Then appends this to the chat returned. Yet I keep getting the sender object. I hope I'm able to clearly express the problem? How do I get the receiver object?

NB: Pls I'm sending this from a mobile phone so it may not be properly formatted.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire