Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

Console Commands

Sorry guys, having a total meltdown/brain freeze here and getting myself mixed up.

I'm basically wanting to shove this bunch of code into the background and then schedule it within Forge to run every night.

        if ($this->campaign->updated_at != $this->days && $this->campaign->updated_at <= $this->days) {
            $data = array(
                'name' => Auth::user()->first_name,
                'email' => Auth::user()->email,
                'campaign' => $this->campaign->campaign_name
            );
            // Send the email
            Mail::send('emails/campaigns/overdue', $data, function( $message ) use ($data)
            {
                $message->to($data['email'])
                    ->from('noreply@domain.com')
                    ->subject('Campaign inactive for more than 7 days');
            });
        }

How should I do this? I believe a console command, but unsure how I can pass the code above through to it, then be able to test this locally using Beanstalkd?

Help/advise greatly appreciated.

0 likes
1 reply
theUnforgiven's avatar

I have the above stored in a job class like so:

<?php

namespace App\Jobs;

use App\Campaigns;
use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;

class CampaignOverdue extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    /**
     * @var Campaigns
     */
    public $campaign;
    /**
     * @var
     */
    private $days;
    /**
     * @var
     */
    private $now;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if ($this->campaign->updated_at != $this->days && $this->campaign->updated_at <= $this->days) {
            $data = array(
                'name' => Auth::user()->first_name,
                'email' => Auth::user()->email,
                'campaign' => $this->campaign->campaign_name
            );
            // Send the email
            Mail::send('emails/campaigns/overdue', $data, function( $message ) use ($data)
            {
                $message->to($data['email'])
                    ->from('noreply@domain.com')
                    ->subject('Campaign inactive for more than 7 days');
            });
        }
    }
}

But question is how do I pass this to a console command class, so i can set it run within Forge.

Please or to participate in this conversation.