【发布时间】:2018-10-23 11:22:46
【问题描述】:
我正在建立一个网站,用户可以在其中购买他们需要的任何预计时间的计划。在他们购买计划后,我创建了一个中间件来检查计划何时几乎完成(计划中的 70%)然后发送通知。现在的问题是我试图在发送另一个通知之前确定是否已经发送了相关通知。我使用了数据库通知方法。
为了实现这一点,我必须创建一个通知模型来与我的通知表进行交互。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
protected $table= "notifications";
}
有问题的中间件
public function handle($request, Closure $next)
{
// dd(Carbon::now()->format("Y-m-d"));
$plan = Booking::where("user_id", Auth::id())
->where("approve", true)
->where("end_date", '>', Carbon::now()->format("Y-m-d"))
->get();
$i =0;
$save = [];
foreach($plan as $plan){
$save[$i] = (new Plan)->percentageMeter($plan->start_date, $plan->end_date);
if($save[$i] >= 70){
$realNotify= Notification::where("notifiable_id", $plan->user_id)
->get();
foreach($realNotify as $notify){
if($notify->data['plan_id'] == $plan->id && realNotify ==true ){
continue;
}
else{
ComNotify::planAboutToExpire(Booking::find($plan->id));
}
}
continue;
}
$i++;
}
// dd($save);
}
我遇到此问题的主要原因之一是我无法访问“plan_id”对象,因为它没有自己的表列。
有没有一种 Laravel 方法可以做到这一点?一个整洁的方式。
public function toArray($notifiable)
{
return [
"type"=>"plan_about_to_expire",
"message"=>"Your plan will soon expire on " . $this->plan->end_date,
"plan_id"=>$this->plan->id,
];
}
我的通知 toArray 方法
【问题讨论】:
-
处理它的最合乎逻辑的方法是在计划表上创建一个列,并将其用作标志。在发送通知之前,检查标志是否设置,如果没有,发送通知并设置标志。
-
是的,我做了一些事情来实现预期的目标,只是创建了一个名为提醒的单独表并将其与 Plan_id 一起放在那里。 @乔
标签: php laravel-5 web frameworks