zubair malik
zubair malik

Reputation: 223

I want a notification that date is going to expire next week using laravel

i am using Task Scheduling for notification Now I am getting all notification from work permit table but I want a notification that date is going to expire next week please help me how can i do that ? thanks.

work permit table

id | name | permit_code | date

notification table

id | permit_id

app/console/commands/CreatePermitWork

class CreatePermitWork extends Command
{
  /**
   * The name and signature of the console command.
   *
   * @var string
   */
  protected $signature = 'notifications:create';

  /**
   * The console command description.
   *
   * @var string
   */
  protected $description = 'Permit work notification added !';

  /**
   * Create a new command instance.
   *
   * @return void
   */
  public function __construct()
  {
    parent::__construct();
  }

  /**
   * Execute the console command.
   *
   * @return int
   */
  public function handle()
  {
    $workpermits = WorkPermit::all();
        
    foreach ($workpermits as  $permit) {
      $ids =  array(['permit_id' => $permit->id]);
      $this->info("inserting data ".$permit->name);
      FacadesDB::table('work_permit_notifications')->insert($ids);
    }
  }
}

Upvotes: 1

Views: 1237

Answers (2)

user4796879
user4796879

Reputation:

$workpermits = WorkPermit::all();

Will give you all Work Permits

$workpermits = WorkPermit::where('date', '<', now()->addDays(7))->get();

Will give you work permits that have a date larger than smaller than 7 days from now.

You can also simplify the foreach and use the collection instance

WorkPermit::where('date', '<', now()->addDays(7))
  ->get()
  ->each(function ($workpermit) {
        DB::table('work_permit_notifications')->insert($workPermit->id);
    });

Upvotes: 2

Hamid
Hamid

Reputation: 384

You must get these records first
$havoToNotif = YourModel::where('date','<',Carbon::now()->addDays(7))->get();

Upvotes: 2

Related Questions