I put what @JamesMills stated in my model:
<?php
namespace App\Models;
use Auth;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//
protected $fillable = ['body'];
protected $appends = ['likeCount', 'likedByCurrentUser', 'canBeLikedByCurrentUser'];
protected $with = [
'user',
];
......
In addition, @socieboy solution:
Here is my event:
class PostWasCreated implements ShouldBroadcast
{
use InteractsWithSockets, SerializesModels;
public $post;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Post $post)
{
$this->post = $post;
}
public function broadcastWith()
{
return [
'post' => $this->prepareData(),
];
}
protected function prepareData()
{
return [
'apple' => $this->post->load(['user']),
'user' => "hello"
];
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('postsIT');
}
}
I used the word apple just to see if the payload would be different going through instead of post as it should be.
my controller method:
public function store(Request $request)
{
$this->validate($request, [
'body' => 'required'
]);
$post = $request->user()->posts()->create([
'body' => $request->body
]);
broadcast(new PostWasCreated($post))->toOthers();
return response()->json([
'data' => $post->load(['user'])
], 200);
}
Here is the data and error I'm getting in the browser:
Pusher : Event recd : {"event":"App\\Events\\PostWasCreated","data":{"post":{"id":96,"user_id":2,"body":"rigged","created_at":"2016-10-28 14:05:23","updated_at":"2016-10-28 14:05:23","likeCount":0,"likedByCurrentUser":false,"canBeLikedByCurrentUser":false,"likes":[]}},"channel":"private-postsIT"}
vue.common.js?4a36:1019 [Vue warn]: Error when evaluating expression "post.user.name": TypeError: Cannot read property 'name' of undefined (found in component: <post>)warn @ vue.common.js?4a36:1019Watcher.get @ ?d41d:3176Watcher @ ?d41d:3158Directive._bind @ ?d41d:8345linkAndCapture @ ?d41d:6928compositeLinkFn @ ?d41d:6897Vue._compile @ ?d41d:8611Vue.$mount @ ?d41d:9446Vue._init @ ?d41d:2468Post @ VM9390:2build @ ?d41d:5837mountComponent @ ?d41d:5754(anonymous function) @ ?d41d:5716(anonymous function) @ ?d41d:5734cb @ vue.common.js?4a36:367Vue._resolveComponent @ ?d41d:8869resolveComponent @ ?d41d:5736setComponent @ ?d41d:5715bind @ ?d41d:5675Directive._bind @ ?d41d:8325linkAndCapture @ ?d41d:6928compositeLinkFn @ ?d41d:6897Fragment @ ?d41d:3757FragmentFactory.create @ ?d41d:3974create @ ?d41d:4210diff @ ?d41d:4109update @ ?d41d:4042_update @ ?d41d:8337Watcher.run @ ?d41d:3340runBatcherQueue @ ?d41d:3071flushBatcherQueue @ ?d41d:3041nextTickHandler @ vue.common.js?4a36:445
vue.common.js?4a36:1019
[Vue warn]: Error when evaluating expression ""/channel/"+(post.user.channel[0].slug)": TypeError: Cannot read property 'channel' of undefined (found in component: <post>)
Why am I not getting apple and user in the response?
this is how my vue look like:
events: {
'child-msg': function (postIt) {
this.posts.unshift(postIt);
}
}
Echo.private('postsIT').listen('PostWasCreated', (e) => {
this.$dispatch('child-msg', e);
});