I am trying to send a notification to the enrolled users in an event a day before the event’s start date. I have a user table events table that contains the start date of each event. Also, I have an enrollment table that includes the user_id and event_id as foreign keys and the start date of these events. However, while I was trying to code the logic of this function and executing it, I got this error.
<?php
namespace AppConsoleCommands;
use AppModelsEnrollment;
use IlluminateConsoleCommand;
use AppModelsUser;
use AppModelsevent;
use IlluminateSupportFacadesDB;
use CarbonCarbon;
use IlluminateNotificationsNotifiable;
class SendNotification extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:notification';
/**
* The console command description.
*
* @var string
*/
protected $description = 'send automatic notification to users that enrolled in events a day before the event date';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// $users = User::all();
// dd(count($users));
$enrolls = Enrollment::all()->keyBy('user_id')->all();
$events = DB::select('SELECT start_date , user_id FROM events , enrollments where events.id= event_id');
foreach($events as $event) {
$created = new Carbon($event->start_date);
$now = Carbon::now();
if((($created->diffInHours($now))>=24 )&& (($created->diffInHours($now))<=48)){
//$user = User::where('id', $event->user_id['user_id'])->first();
$user = User::where('id', isset($event->user_id))->first();
// $user = DB::select('SELECT * FROM users where id='.$event->user_id);
//dd($user);
$user->notify("Your deadline is in day!");
}
}
}
}