I am building a site where users can buy a plan for whatever estimated time they require. After they buy the plan I created a middleware to check when the plan is almost finished (70% into the plan) then send a notification . Now the issue is I am trying to find out if the Notification in question has already been sent before sending another notification. I used database notification method.
To achieve this I had to create a Notification model to interact with my notification table.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
protected $table= "notifications";
}
The middle ware in question
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] >= 15){
$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);
}
one of the major reason I have this problem is that I cant access the "plan_id" object because it does not have its own table column.
Is there a Laravel way to do this? a neat way.
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,
];
}
My notification toArray method
Aucun commentaire:
Enregistrer un commentaire