0

I have an system that lets a user import their bank statements as CSV file. The import is queued as a job in the background. However, I want to notify the user to let them know when their import is finished. How can I do this with Laravel queues and notifications?

Update:

I have tried getting the JobProcessed $event to the AppServiceProvider@boot method to get the command that is running, with the following code:

Queue::after(function (JobProcessed $event) {
    $cmd = unserialize( json_decode( $event->job->getRawBody() )->data->command );
    dd( $cmd );
});

This returns the following dump. However, I don't know how to fetch the account > attributes > user_id:

App\Jobs\TestJob {#730
  #account: App\Account {#750
    #fillable: array:3 [
      0 => "name"
      1 => "number"
      2 => "bank"
    ]
    #connection: "sqlite"
    #table: "accounts"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:7 [
      "id" => "1"
      "created_at" => "2019-12-12 12:25:16"
      "updated_at" => "2019-12-12 12:25:16"
      "user_id" => "1"
      "name" => "Customer Name"
      "number" => "Bank Number"
      "bank" => "Bank Name"
    ]
    #original: array:7 [
      "id" => "1"
      "created_at" => "2019-12-12 12:25:16"
      "updated_at" => "2019-12-12 12:25:16"
      "user_id" => "1"
      "name" => "Customer Name"
      "number" => "Bank Number"
      "bank" => "Bank Name"
    ]
    #changes: []
    #casts: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #guarded: array:1 [
      0 => "*"
    ]
  }
  #job: null
  +connection: null
  +queue: null
  +chainConnection: null
  +chainQueue: null
  +delay: null
  +middleware: []
  +chained: []
}

Update 2:

Solved. My job declares $this->account as protected, and it needs to be set public for it to be accessible.

rebellion
  • 6,628
  • 11
  • 48
  • 79

1 Answers1

0

See the updates in the original entry to see the answer.

Basically, use job events and make the data you need to access public in the job.

rebellion
  • 6,628
  • 11
  • 48
  • 79