Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Laravel 5.2发送通知_Laravel_Events_Notifications_Queue - Fatal编程技术网

如何使用Laravel 5.2发送通知

如何使用Laravel 5.2发送通知,laravel,events,notifications,queue,Laravel,Events,Notifications,Queue,我有一个求职网站,用户可以关注一家公司,然后每当有新工作时,他都会收到通知,所以我做了以下工作: 我创建了一个名为JobNotification的模型,每当公司创建新作业时,我创建一个作业通知,我存储公司id、作业id和生成的文本,因此此作业通知将发送给用户 接下来我做的是创建名为SendJobNotification的作业,并在JobNotification创建之后发送它,SendJobNotification将获取跟踪该公司的用户,并插入用户id和job_notification_id,因此

我有一个求职网站,用户可以关注一家公司,然后每当有新工作时,他都会收到通知,所以我做了以下工作:

我创建了一个名为JobNotification的模型,每当公司创建新作业时,我创建一个作业通知,我存储公司id、作业id和生成的文本,因此此作业通知将发送给用户

接下来我做的是创建名为SendJobNotification的作业,并在JobNotification创建之后发送它,SendJobNotification将获取跟踪该公司的用户,并插入用户id和job_notification_id,因此当用户再次登录时,他将看到通知

所以问题是,有没有更好的办法?也许使用事件? 我不想使用队列:在服务器中侦听以运行队列,而且用户获取占用了很多时间。 这是我的SendJobNotification作业代码:

namespace App\Jobs;
use App\AccountJobNotification;
use App\Jobs\Job;
use App\Account;
use App\JobNotification;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Company;
class SendJobNotification extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $notification;

    public function __construct(JobNotification $notification)
    {
        $this->notification = $notification;

    }  
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $users = Company::find($this->notification->company_id)->followers()->get();
        foreach ($users as $user)
        {
            $job_notification = new AccountJobNotification();
            $job_notification->user_id = $user->id;
            $job_notification->job_notification_id = $this->notification->id;
            $job_notification->save();
        }


    }
}

请注意,我不是在做实时通知。

是的,您可以使用事件和侦听器来做。 我会这样做:
在控制器中,触发事件:

   Event::fire(new JobCreated($job));
然后添加事件(JobCreated.php)和侦听器(AddNotifications.php):

在事件文件夹中创建:JobCreated.php:

<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class JobCreated extends Event
{
    public function __construct($job)
    {
        $this->job_id=$job->id;
        $this->company_id=$job->company_id;
    }
}

是的,您可以通过使用事件和侦听器来完成。 我会这样做:
在控制器中,触发事件:

   Event::fire(new JobCreated($job));
然后添加事件(JobCreated.php)和侦听器(AddNotifications.php):

在事件文件夹中创建:JobCreated.php:

<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class JobCreated extends Event
{
    public function __construct($job)
    {
        $this->job_id=$job->id;
        $this->company_id=$job->company_id;
    }
}

@费伦的回答是正确的。要在队列中添加此事件,必须实现如下所示的ShouldQueue接口

 <?php

    namespace App\Events;

    use App\Events\Event;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
    use Illuminate\Contracts\Queue\ShouldQueue;


    class JobCreated extends Event implements ShouldQueue
    {
        public function __construct($job)
        {
            $this->job_id=$job->id;
            $this->company_id=$job->company_id;
        }
    }
在linux体系结构上,在结束代码命令处添加“>/dev/null 2>&1”,以便在不等待的情况下运行它

php artisan queue:work beanstalkd --sleep=3 --tries=3 --daemon > /dev/null 2>&1 & 
这里beanstalkd是一个队列驱动程序,也可以使用supervisor。使用以下链接了解更多信息


费伦的回答是正确的。要在队列中添加此事件,必须实现如下所示的ShouldQueue接口

 <?php

    namespace App\Events;

    use App\Events\Event;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
    use Illuminate\Contracts\Queue\ShouldQueue;


    class JobCreated extends Event implements ShouldQueue
    {
        public function __construct($job)
        {
            $this->job_id=$job->id;
            $this->company_id=$job->company_id;
        }
    }
在linux体系结构上,在结束代码命令处添加“>/dev/null 2>&1”,以便在不等待的情况下运行它

php artisan queue:work beanstalkd --sleep=3 --tries=3 --daemon > /dev/null 2>&1 & 
这里beanstalkd是一个队列驱动程序,也可以使用supervisor。使用以下链接了解更多信息


既然您要求提供一种发送此通知的方法,而该方法不需要队列工作程序(顺便说一句,我认为这是正确的方法),那么这里有一种使用数据库通知的方法

您已经通过使用事件侦听器/处理程序获得了示例的答案,因此这里有一种使用本机雄辩事件和服务提供者的方法

旁白:服务提供商不是必需的,但我喜欢包含所有 我的模型事件包含在它们中,所以我不会在查看时把头发拔出来 试图查找错误创建/删除事件的模型

您需要先进行一些设置:

php artisan make:provider JobListingServiceProvider
php artisan make:notification JobListingWasCreated
php artisan notifications:table
php artisan migrate
…您需要在
config/app.php
中编辑提供商列表,以包含新的提供商

App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\JobListingServiceProvider::class, //<-- New one
我在那里使用
chunk()
而不是
each()
,因为您可能要处理数千条用户记录(如文档所建议的),其中有一个
orderBy('id')
,因为
chunk()
要求构建器中有一个
orderBy
子句


joblistingwassecreated因为您要求提供一种发送此通知的方法,该方法不需要队列工作程序(顺便说一句,我认为这是正确的方法),下面是一种使用数据库通知的方法

您已经通过使用事件侦听器/处理程序获得了示例的答案,因此这里有一种使用本机雄辩事件和服务提供者的方法

旁白:服务提供商不是必需的,但我喜欢包含所有 我的模型事件包含在它们中,所以我不会在查看时把头发拔出来 试图查找错误创建/删除事件的模型

您需要先进行一些设置:

php artisan make:provider JobListingServiceProvider
php artisan make:notification JobListingWasCreated
php artisan notifications:table
php artisan migrate
…您需要在
config/app.php
中编辑提供商列表,以包含新的提供商

App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\JobListingServiceProvider::class, //<-- New one
我在那里使用
chunk()
而不是
each()
,因为您可能要处理数千条用户记录(如文档所建议的),其中有一个
orderBy('id')
,因为
chunk()
要求构建器中有一个
orderBy
子句


JobListing已创建谢谢您的回答,这个队列可以排队吗?它能解决延迟过程吗?谢谢你的回答,这个队列可以吗?它会解决延迟过程吗?谢谢,但我不明白,我在哪里可以找到用户,我应该做作业?谢谢,但我不明白,我在哪里可以找到用户,我应该做作业?
class User extends Authenticatable
{
    use Notifiable;

    public function companies()
    {
        return $this->belongsToMany(Company::class);
    }
}
class Company extends Model
{
    public function addJobListing($listingTitle)
    {
        $this->jobListings()->save(
            new JobListing(['title' => $listingTitle])
        );
    }

    public function jobListings()
    {
        return $this->hasMany(JobListing::class);
    }

    public function followers()
    {
        return $this->belongsToMany(User::class);
    }
}
    Schema::create('company_user', function(Blueprint $table){
        $table->integer('user_id')->unsigned();
        $table->integer('company_id')->unsigned();
    });