gerardojbaez / messenger

Chat/Message system for Laravel.
MIT License
72 stars 24 forks source link

Get it with Vuejs... #4

Closed tutkun closed 6 years ago

tutkun commented 7 years ago

Hey, thanks for this pack!

How I get the messages with sender for vuejs?

I use:

/**
 * Display the specified resource.
 *
 * @param  \Gerardojbaez\Messenger\Models\Message  $message
 * @return \Illuminate\Http\Response
 */
public function show(Message $message)
{
    return response()->json($message, 200);
}

but, i want that is like;

public function show(Message $message)
{
    return response()->json($message->with('users'), 200);
}
gerardojbaez commented 6 years ago

Sorry for being late. I forgot to reply your question.

Each message belongs to one user (the sender): $message->sender.

To get all messages in a particular thread, with senders, use this:

/**
 * List all messages.
 *
 * @param  \Gerardjbaez\Messenger\Models\MessageThread $thread
 * @return Response
 */
function all(MessageThread $thread)
{
    // This will load all messages with sender
    $thread->load('messages.sender');

    return response()->json([
        'messages' => $thread->messages->toArray()
    ]);
}

To get one message with sender, use this:

/**
 * Show a message.
 *
 * @param  \Gerardjbaez\Messenger\Models\Message $message
 * @return Response
 */
function show(Message $message)
{
    // This will load sender
    $message->load('message.sender');

    return response()->json([
        'message' => $message->toArray()
    ]);
}