Laravel 5.1 Highlights

I am using Laravel Model Events. My requirement is to pass additional parameters to the event.

I try so hard:

$feedback = new Feedback();
    $feedback->user_id = $this->user_id;
    $feedback->feedback = $request->feedback;
    $data = array(
        'message' => $request->feedback,
        'from' => $this->data->user->email,
        'name' => $this->data->user->displayname
    );
    $feedback->save($data);

My event:

public function boot()
{
    Feedback::saved(function ($item) {
        //\Event::fire(new SendEmail($item));
    });
}

But it only sends the Model object while I try to send:

$data = array(
        'message' => $request->feedback,
        'from' => $this->data->user->email,
        'name' => $this->data->user->displayname
    );

How to send this event?

+4
source share
1 answer

There are definitely ways to solve this problem. The first thing that comes to mind is to get the Authdata inside Providerwhere your event lives.

You will need to do something like this:

use Auth; //Assuming this is how you are handling authentication

public function boot()
{
    Feedback::saved(function ($item) {
        $user = Auth::user();
        $data = [ 
            'message' => $item->feedback, 
            'from' => $user->email, 
            'name' => $user->displayname
        ];
        \Event::fire(new SendEmail($data));
    });
}

Instead, you can do it $item->user->emailand not worry about Auth, I just can't know the relationship with what you posted so far.

, , - !

+2

Source: https://habr.com/ru/post/1616538/


All Articles