Return nested array laravel event

Viewed 216

I'm, trying to return a nested array through an Event. But I'm not pretty sure how this would work:

This is what I got in my controller:

public function send(Request $request, Group $group){

        $message = new Message([
            'user_id' => Auth::user()->id,
            'group_id' => $request->groupId,
            'message' => $request->message
        ]);
        $message->save();

        event(new MessageSent($message));

        return response()->json([
        "code" => 200,
        "message" => "Message successfully sent"
        ]);
        
    }

this is what I got in my event:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use App\Http\Resources\Group as GroupResource;
use App\Http\Resources\Message as MessageResource;

use App\Message;
use App\User;

class MessageSent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;


    public function __construct(Message $message)
    {
        $this->message = $message;
        $arr = $message->toArray();
        $user = User::where('id', $this->message->user_id)->first();
        $arr['user'] = $user->toArray();

        return $arr;
    }

    public function broadcastOn()
    {
        return new Channel('group.' . $this->message->group->id);
    }

}

When the event is triggered, I'm only getting the Message model,but not the return $arr;

my goal is to return the array the following way, this is an edit to ask if the provided solution won't affect this structure as the solution makes the array to provide object inside object, but there might be a difference:

{
  "data": {
    "_id": 1, // ID of the message
    "text": "test", // content of the message
    "createdAt": "timestamp", 
    "user": [ // data of the user
      {
        "_id": 1,
        "name": "User name",
        "email": "User email",
        "created_at": "timestamp",
        "updated_at": "timestamp"
      }
    ]
  }
}
1 Answers

You must declare you var in the MessageSent class, try this:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use App\Http\Resources\Group as GroupResource;
use App\Http\Resources\Message as MessageResource;

use App\Message;
use App\User;

class MessageSent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;
    public $data;


    public function __construct(Message $message)
    {
        $this->message = $message;
        $user = User::where('id', $this->message->user_id)->first();
        $this->message->user = $user;
        $this->data= $message->toArray();

    }

    public function broadcastOn()
    {
        return new Channel('group.' . $this->message->group->id);
    }

}

In this way, you can retrieve the array data

Related