我想通过推送程序将通知发送给特定用户。 I've installed everything needed already
user_id
(所有者)project_id
user_id
(出价)我想发生的是
当用户
bid on a project
时,project owner
会在推送程序的帮助下收到有关该操作的通知。
Echo.channel
中创建了app.js
进行测试我需要您帮助的
要完成我的事件功能并实际在浏览器中获取数据 特定用户
the project owner
event
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\User;
use App\Bid;
class BidPlaced implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public $bid;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(User $user, Bid $bid)
{
$this->user = $user;
$this->bid = $bid;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return ['bidplaced.'.$this->bid->project_id];
}
}
controller
public function sendbid(Request $request)
{
//validator
$bid = new Bid;
//input data
$bid->save();
//send the notification to project owner
broadcast(new BidPlaced($bid))->toOthers();
Session::flash('sucess', 'Your Bid placed successfully.');
return redirect()->back();
}
User Model
public function projects()
{
return $this->hasMany(Project::class);
}
public function bids()
{
return $this->hasMany(Bid::class);
}
Project Model
public function User()
{
return $this->belongsTo(User::class);
}
public function bids()
{
return $this->hasMany(Bid::class);
}
Bid Model
public function user()
{
return $this->belongsTo(User::class);
}
public function project()
{
return $this->belongsTo(Project::class);
}
我应该怎么做才能将通知发送给项目所有者,因为他/她对该项目有新的出价?
我进行了一些更改,到目前为止,我已经在pusher中获得了数据,但是在我的应用程序中什么也没有收到(无法获得通知)
blade
添加了身份验证用户ID。
<notifications user_id="{{ auth()->user()->id }}"></notifications>
broadcast routes
Broadcast::channel('bidplaced.{UserId}', function ($user, $UserId) {
return (int) $user->id === (int) $UserId;
});
Event
获取项目所有者ID作为第二个参数(我在路线中称为$UserId
,
return new PrivateChannel('bidplaced.'.$this->bid->project->user->id);
Vue Component
在这里我添加了+this.user_id
,它从刀片服务器which should be the same as my event return changes
获取Auth用户ID
Echo.private('bidplaced.'+this.user_id)
.listen('BidPlaced', (e) => {
this.bids.push(e.bid);
});
Results
这就是我在pusher中获得的结果(但无法显示在我的视图中)
{
"bid": {
"id": 240,
"user_id": 2, // worker id
"project_id": 31,
"bid": 6666666,
"message": "yhyhyhyhyhyhyhyhyhyhyhyhyhyh",
"attachment": null,
"accepted": "n",
"created_at": "2018-08-06 01:06:33",
"updated_at": "2018-08-06 01:06:33",
"project": {
"id": 31,
"user_id": 1, // id of the owner
"title": "number 31",
"slug": "number-31",
"body": "<p>frgkljelighojte </p>",
"attachment": null,
"projectclass": "fgheth",
"budget": 45346,
"deadline": "2018-08-23",
"published": "n",
"runing": "n",
"payment_verified": "n",
"created_at": "2018-08-04 17:27:47",
"updated_at": "2018-08-04 17:27:47",
"user": { //the owner info
"id": 1,
"name": "admin",
"email": "admin@admin.com",
"created_at": "2018-08-02 10:57:47",
"updated_at": "2018-08-02 10:57:47"
}
}
}
}
有什么想法吗?