0

I have a Laravel application where I use Laravel event and Listeners. In my real use case, whenever a user visits the Invoice details page I want to execute two events. One of them actually inserts some related data for the invoice.

but on the display page, that data is not showing(which was inserted by events) however data is inserted into the table successfully

/**
 * Display the specified resource.
 *
 * @param  \App\Invoice  $invoice
 * @return \Illuminate\Http\Response
 */
public function show(Invoice $invoice)
{
    event(new EventA($invoice->user));
    event(new EventB($invoice));
    return view('invoices.'.$invoice->type.'.show', compact('invoice'));
}

What I want is to execute these two events and wait until they complete their job and then render the details page.

It will be better if someone helps me know how we can return data from the event execute statements like

eventAExecuted = event(new EventA($invoice->user)); // expect true
eventBExecuted = event(new EventB($invoice)); // expect true

so then I can perform the boolean expression on these

if (eventAExecuted && eventBExecuted) {
    return view('invoices.'.$invoice->type.'.show', compact('invoice'));
}

UPDATED

I return true from the Listeners which return the following output

Array ( [0] => 1 [1] => 1 ) Array ( [0] => 1 )

So I apply boolean expression but the data which I inserted through listeners is inserted into the table but does not print in the controller at the first time.

eventAExecuted = event(new EventA($invoice->user)); // expect true and insert hasMany realtion data. for eg., invoice->transactions
eventBExecuted = event(new EventB($invoice)); // expect true

if ($eventAExecuted && $eventBExecuted) {
    print_r($invoice->transactions); // getting empty collection however data is inserted successfully.
    die;
    return view('invoices.'.$invoice->type.'.show', compact('invoice'));
}
Sachin Kumar
  • 3,001
  • 1
  • 22
  • 47
  • If your event listener implements `ShouldQueue` it should not implement that anymore – apokryfos Jan 28 '21 at 05:50
  • I did not implement ShouldQueue and also QUEUE_CONNECTION=database. but this is a sync event. – Sachin Kumar Jan 28 '21 at 05:54
  • You can return results form a sync event. The only caveat is that the return value of `event` is an array which contains a the result from all event listeners (because it can be listened to by multiple event listeners). If you know that only one listener will listen to the event you can use `Event::until` to only get the first listener result – apokryfos Jan 28 '21 at 06:14
  • Yes, I check the output. It is in the array form like array([0] => ) see nothing. However, I have registered two listeners for one event and one for another event. The return value is meaningless. – Sachin Kumar Jan 28 '21 at 06:18
  • and also provide me the link for reading Event::until as I didn't find it. BTW I am using laravel 7 version. – Sachin Kumar Jan 28 '21 at 06:22
  • There's no direct documentation for that, the code is [here](https://github.com/laravel/framework/blob/8.x/src/Illuminate/Events/Dispatcher.php#L208) and the `Event` facade calls that function (the `event()->until` might also work since that works similarly to a facade). I use an [ide-helper](https://github.com/barryvdh/laravel-ide-helper) to actually see all the methods available in a facade, so that's probably how I've first seen it (or maybe it was documented in the past I can't remember). Are you actually doing any `return true` in your event handlers though? – apokryfos Jan 28 '21 at 06:40
  • I return true from the listeners and now I get Array ( [0] => 1 [1] => 1 ) Array ( [0] => 1 ) but the problem is I still not getting the related data(in blade file) which I inserted through listener. However the data is inserted into tables. – Sachin Kumar Jan 28 '21 at 07:24
  • 2
    You might need to re-retrieve the data since the model instance you have in the controller does not get updated by whatever happens in the events. Maybe try `$invoice->load('transactions')` after the events finish – apokryfos Jan 28 '21 at 07:35
  • Thank you, man. This is what I am looking for. $invoice->load('transactions') does the trick. – Sachin Kumar Jan 28 '21 at 07:37

0 Answers0