简体   繁体   中英

Laravel echo and relationships wrong data in event

my problem is that i can't seem to return a newly created record with it's relationships to a event.

i have this store method.

public function store(Request $request){
$post = new Post($request->all());
$post->user_id = Auth::user()->id;
$post->save();

$createdPost = Post::with('owner')->findOrFail($post->id);
event(new PostCreatedEvent($createdPost));
return $createdTicket;
}

now i expect this to return data like

{
"post": {
"id": 10,
"title": '"hello",
"body": "world",
"user_id": 1,
"created_at": date,
"updated_at": date,
"owner": {
        "id": 1,
        "name": "name",
        "email": "email@example.com"
    }
  }
}

And i do get this from the return in the store method. But in the event that gets sent to pusher i get this

{
    "post": {
        "id": 10,
        "title": '"hello",
        "body": "world",
        "user_id": 1,
        "created_at": date,
        "updated_at": date,
    }
}

My question is why does the event strip out the owner data from before sending it to pusher and how can i fix it.

It seems like there is something happening with the data when it gets to the event class. After not being able to fix it i tried some random solutions.

$createdPost = Post::with('owner')->findOrFail($post->id)->toJson();

This did the trick. I now need to json parse it on the frontend but all the data is coming through.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM