Skip to content

Commit 0e28f45

Browse files
author
Mostafa Kamal
committed
event with queue
1 parent 63a2acb commit 0e28f45

File tree

1 file changed

+118
-4
lines changed

1 file changed

+118
-4
lines changed

web-developer/laravel/tips/events.md

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ php artisan make:listener ListenerName
1818
php artisan make:event EventName
1919
```
2020

21-
# event
21+
## event
2222

2323
* `EventName` class.
2424

@@ -63,7 +63,7 @@ class EventName
6363
}
6464
```
6565

66-
# listen
66+
## listener
6767

6868
```php
6969

@@ -98,7 +98,7 @@ class ListenName
9898
}
9999
```
100100

101-
# register
101+
## register
102102

103103
* `app\Providers\EventServiceProvider.php`
104104

@@ -112,13 +112,127 @@ protected $listen = [
112112
];
113113
```
114114

115-
# fire / dispatching
115+
## fire / dispatching
116116

117117
```php
118118
use App\Events\OrderShipped;
119119
event(new EventName($order));
120120
```
121121

122+
## event with queue
123+
124+
* table create
125+
126+
```bash
127+
php artisan queue:table
128+
php artisan migrate
129+
```
130+
* update inside .env for (database)
131+
* `QUEUE_CONNECTION=database`
132+
133+
* `implements ShouldQueue ` in `ListenName` class
134+
135+
```php
136+
namespace App\Listeners;
137+
138+
use App\Events\EventName;
139+
use Illuminate\Queue\InteractsWithQueue;
140+
use Illuminate\Contracts\Queue\ShouldQueue;
141+
142+
class ListenName implements ShouldQueue
143+
{
144+
/**
145+
* Create the event listener.
146+
*
147+
* @return void
148+
*/
149+
public function __construct()
150+
{
151+
//
152+
}
153+
154+
/**
155+
* Handle the event.
156+
*
157+
* @param EventName $event
158+
* @return void
159+
*/
160+
public function handle(EventName $event)
161+
{
162+
return $event;
163+
}
164+
}
165+
```
166+
167+
* `php artisan queue::work`
168+
169+
## event with queue `2nd method`
170+
171+
* table create
172+
173+
```bash
174+
php artisan queue:table
175+
php artisan migrate
176+
```
177+
178+
* `implements ShouldQueue ` in `ListenName` class
179+
180+
```php
181+
namespace App\Listeners;
182+
183+
use App\Events\EventName;
184+
use Illuminate\Queue\InteractsWithQueue;
185+
use Illuminate\Contracts\Queue\ShouldQueue;
186+
187+
class ListenName implements ShouldQueue
188+
{
189+
/**
190+
* The name of the connection the job should be sent to.
191+
*
192+
* @var string|null
193+
*/
194+
public $connection = 'database';
195+
196+
/**
197+
* The name of the queue the job should be sent to.
198+
*
199+
* @var string|null
200+
*/
201+
public $queue = 'listeners';
202+
203+
/**
204+
* The time (seconds) before the job should be processed.
205+
*
206+
* @var int
207+
*/
208+
public $delay = 60;
209+
210+
/**
211+
* Create the event listener.
212+
*
213+
* @return void
214+
*/
215+
public function __construct()
216+
{
217+
//
218+
}
219+
220+
/**
221+
* Handle the event.
222+
*
223+
* @param EventName $event
224+
* @return void
225+
*/
226+
public function handle(EventName $event)
227+
{
228+
return $event;
229+
}
230+
}
231+
```
232+
233+
* `php artisan queue::work database --queue=listener`
234+
235+
122236

123237

124238
# Resources

0 commit comments

Comments
 (0)