0

我正在将请求传递给工作类,以发送电子邮件以获取联系表。问题是我无法直接访问该属性。

我将构造函数中的 $request 设置为$this->request = $request;

handle()方法中,我这样做:

\Log::info('job-contact-request', ['request' => $this->request]);

日志吐出:

[2018-06-30 14:07:34] local.INFO: request {"request":"[object] (App\Events\ContactRequestValidated: {\"request\":{\"client_name\":\"Daniel \",\"client_email\":\"email@email.com\",\"client_phone\":\"1234567891\",\"client_text\":\"blargle carble darble zarble\"},\"socket \“:无效的})”}

这表明 client_email 已设置。但是当我尝试访问时$this->request->client_email,我得到:

[2018-06-30 14:15:30] local.ERROR: 未定义属性:App\Events\ContactRequestValidated::$client_email {"exception":"[object] (ErrorException(code: 0): Undefined property: App\ Events\ContactRequestValidated::$client_email at /var/www/site/laravel/app/Jobs/SendContactRequestEmail.php:36) [stacktrace]

4

1 回答 1

0

只是弄乱了 Laravel 事件/听众/工作,有几件事我必须纠正。拥有跨多个类的日志真的让我失望。

在 Event 构造函数中,我必须all()在设置请求时使用该方法。

$this->request = $request->all()通过属性名称正确访问数据。

// Event Constructor
public function __construct( ContactRequest $request )
    {
        $this->request = $request->all();
        // \Log::info( 'Event-Constructor', [ 'contact_request' => $this->request ] );
    }

事件向侦听器传递一个事件对象,该对象具有事件构造函数中设置的数据作为事件对象的属性。必须通过事件对象访问数据。

// Listener
public function handle( ContactRequestValidated $event )
    {
        // \Log::info( 'listener', [ 'contact-event' => $event->request ] );

        dispatch( new SendContactRequestEmail( $event->request ));
    }

下一部分我不太确定为什么会发生这种情况,但在工作中,请求已从对象更改为数组。

所以这是工作代码:

// Job Constructor
public function __construct( $request )
    {
        $this->request = $request; // Actually an array, not an object
    }

// Job handle
public function handle()
    {
        // \Log::info( 'job-contact-request', [ 'request' => $this->request ] );

        $email = new ConfirmContactRequest( $this->request );

        \Log::info( 'job-contact-request', [ 'email_html' => $email ] );

        // Note request has to be accessed by array notation rather than arrow method
        Mail::to( $this->request['client_email'] )->send( $email );
    }
于 2018-07-01T10:40:22.527 回答