Laravel 5.3。调度作业有奇怪的错误

时间:2016-11-12 14:27:08

标签: notifications jobs laravel-5.3

标题说明了一切,这里是代码

控制器:

public function declineReservation($id){
        $reservation=Reservation::where('id',$id)->with('user','apartment')->get()->first();
        $this->dispatch(new SendMails($reservation));
        $reservation->delete();
        return redirect('reservations')->with('status', 'reservation declined');
}

作业:

<?php

namespace App\Jobs;

use App\Notifications\ReservationApproved;
use App\Notifications\ReservationDeclined;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendMails implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    protected $reservation;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($reservation)
    {
        $this->reservation=$reservation;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $user=$this->reservation->user;
        if($this->reservation->status==1){
            $user->notify(new ReservationApproved($this->reservation));
        }
        else{
            $user->notify(new ReservationDeclined($this->reservation));
        }
    }
}

现在是有趣的部分。 declineReservation方法中的预留状态等于0,因此在作业句柄中,它们会转到else块但是没有发送邮件,除非我删除

$reservation->delete();

来自declineReservation方法。

这是我在堆栈上的第一个问题,所以我希望你们都渴望帮助解决这个变化无常的问题。

1 个答案:

答案 0 :(得分:0)

经过一番思考后我才意识到,因为在同步删除之后稍后触发了作业,所以不能删除它,而不是删除它我设置了我的模型,因此它可以被软件删除&#34;,之后作业被执行(发送邮件)我从数据库中删除记录。

型号:

只需添加此

use SoftDeletes;
protected $dates = ['deleted_at'];

不要忘记导入SoftDeletes或使用完整路径。

迁移:

将其添加到架构构建器

$table->softDeletes();

控制器:

public function declineReservation($id){
     $reservation=Reservation::withTrashed()->where('id',$id)-with('user','apartment')->get()->first();
     $this->dispatch(new SendMails($id));
     $reservation->delete();
     return redirect('reservations')->with('status', 'reservation declined');
}

作业:

<?php

namespace App\Jobs;

use App\Notifications\ReservationApproved;
use App\Notifications\ReservationDeclined;
use App\Reservation;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendMails implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;

protected $param;

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct($param)
{
    $this->param=$param;
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    if(is_object($this->param)){
        $user=$this->param->user;
        $user->notify(new ReservationApproved($this->param));
    }
    else{
        $reservation=Reservation::withTrashed()->where('id',$this->param)->get()->first();
        $reservation->user->notify(new ReservationDeclined($reservation));
        $reservation->forceDelete();
        }
    }
}

在Job句柄中,我检查param是否为对象,因为当预约被批准时我将模型传递给构造函数,并且在下降时我传递模型ID,因此我可以使用withTrashed()方法加载它。在那之后只需要强制删除&#34;模特。

有关软删除的详细信息,请参阅:https://laravel.com/docs/5.3/eloquent#soft-deleting