将laravel事件发送给特定用户

时间:2018-08-04 07:50:27

标签: laravel pusher

我想通过推送程序将通知发送给特定用户。 I've installed everything needed already

逻辑

  1. 用户可以创建项目
  2. 用户可以对项目出价
  3. 项目获得user_id所有者
  4. 出价表获得project_id
  5. 出价获得user_id出价
  

我想发生的是

     

当用户bid on a project时,project owner会在推送程序的帮助下收到有关该操作的通知。

到目前为止,我所做的是:

  1. 创建活动
  2. 保存出价后调用该事件
  3. 在我的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&nbsp;&nbsp;&nbsp;&nbsp;</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"
      }
    }
  }
}

有什么想法吗?

0 个答案:

没有答案