Yasir Pasha
Yasir Pasha

Reputation: 103

Laravel 5.1 Eloquent Events

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

I am trying like that:

$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 is:

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

But it only send Model object while i am trying to sending:

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

How i send this to event?

Upvotes: 4

Views: 272

Answers (1)

Josh Rumbut
Josh Rumbut

Reputation: 2710

There definitely are ways around this problem. The first one that comes to mind is getting the Auth data inside the Provider where your event lives.

You'll 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));
    });
}

You may be able to do $item->user->email instead, and not bother with Auth, I'm just not able to know the relationships from what you've posted so far.

The code might need a little adjustment to work within your application, let me know if anything else comes up!

Upvotes: 2

Related Questions