7
7
[ ![ GitHub] ( https://img.shields.io/github/license/marcreichel/igdb-laravel )] ( https://packagist.org/packages/marcreichel/igdb-laravel )
8
8
[ ![ Gitmoji] ( https://img.shields.io/badge/gitmoji-%20😜%20😍-FFDD67.svg )] ( https://gitmoji.dev )
9
9
10
- This is a Laravel wrapper for version 4 of the [ IGDB API] ( https://api-docs.igdb.com/ ) (Apicalypse) including [ webhook handling] ( #-webhooks-since-v230 ) since version 2.3.0.
10
+ This is a Laravel wrapper for version 4 of the [ IGDB API] ( https://api-docs.igdb.com/ ) (Apicalypse)
11
+ including [ webhook handling] ( #-webhooks-since-v230 ) since version 2.3.0.
11
12
12
13
## Basic installation
13
14
@@ -45,6 +46,11 @@ return [
45
46
*/
46
47
'cache_lifetime' => env('IGDB_CACHE_LIFETIME', 3600),
47
48
49
+ /*
50
+ * Path where the webhooks should be handled.
51
+ */
52
+ 'webhook_path' => 'igdb-webhook/handle',
53
+
48
54
/*
49
55
* The webhook secret.
50
56
*
@@ -58,8 +64,8 @@ return [
58
64
## Usage
59
65
60
66
If you're familiar with the [ Eloquent System] ( https://laravel.com/docs/eloquent )
61
- and the [ Query Builder] ( https://laravel.com/docs/queries ) of Laravel you
62
- will love this package as it uses a similar approach.
67
+ and the [ Query Builder] ( https://laravel.com/docs/queries ) of Laravel you will love this package as it uses a similar
68
+ approach.
63
69
64
70
### Models
65
71
@@ -77,9 +83,8 @@ $games = Game::where('first_release_date', '>=', 1546297200)->get();
77
83
78
84
### Query Builder
79
85
80
- You can use one of the defined models listed above. The search results will be
81
- mapped into the used model automatically then. This method is used in the
82
- examples below.
86
+ You can use one of the defined models listed above. The search results will be mapped into the used model automatically
87
+ then. This method is used in the examples below.
83
88
84
89
Otherwise you can use the Query Builder itself like this:
85
90
@@ -93,10 +98,9 @@ $games = $igdb->get();
93
98
94
99
#### Select
95
100
96
- Select which fields should be in the response. If you want to have all available
97
- fields in the response you can also skip this method as the query builder will
98
- select ` * ` by default. (** Attention** : This is the opposite behaviour from the
99
- Apicalypse API)
101
+ Select which fields should be in the response. If you want to have all available fields in the response you can also
102
+ skip this method as the query builder will select ` * ` by default. (** Attention** : This is the opposite behaviour from
103
+ the Apicalypse API)
100
104
101
105
``` php
102
106
use MarcReichel\IGDBLaravel\Models\Game;
@@ -126,8 +130,8 @@ use MarcReichel\IGDBLaravel\Models\Game;
126
130
$games = Game::where('first_release_date', '>=', 1546297200)->get();
127
131
```
128
132
129
- For convenience, if you want to verify that a column is equal to a given value,
130
- you may pass the value directly as the second argument to the ` where ` method:
133
+ For convenience, if you want to verify that a column is equal to a given value, you may pass the value directly as the
134
+ second argument to the ` where ` method:
131
135
132
136
``` php
133
137
use MarcReichel\IGDBLaravel\Models\Game;
@@ -137,8 +141,8 @@ $games = Game::where('name', 'Fortnite')->get();
137
141
138
142
##### Or Statements
139
143
140
- You may chain where constraints together as well as add ` or ` clauses to the query.
141
- The ` orWhere ` method accepts the same arguments as the ` where ` method:
144
+ You may chain where constraints together as well as add ` or ` clauses to the query. The ` orWhere ` method accepts the same
145
+ arguments as the ` where ` method:
142
146
143
147
``` php
144
148
use MarcReichel\IGDBLaravel\Models\Game;
@@ -160,8 +164,7 @@ $games = Game::whereBetween('first_release_date', 1546297200, 1577833199)->get()
160
164
161
165
###### whereNotBetween
162
166
163
- The ` whereNotBetween ` method verifies that a field's value lies outside of two
164
- values:
167
+ The ` whereNotBetween ` method verifies that a field's value lies outside of two values:
165
168
166
169
``` php
167
170
use MarcReichel\IGDBLaravel\Models\Game;
@@ -171,8 +174,7 @@ $games = Game::whereNotBetween('first_release_date', 1546297200, 1577833199)->ge
171
174
172
175
###### whereIn
173
176
174
- The ` whereIn ` method verifies that a given field's value is contained within the
175
- given array:
177
+ The ` whereIn ` method verifies that a given field's value is contained within the given array:
176
178
177
179
``` php
178
180
use MarcReichel\IGDBLaravel\Models\Game;
@@ -182,7 +184,7 @@ $games = Game::whereIn('category', [0,4])->get();
182
184
183
185
###### whereNotIn
184
186
185
- The ` whereNotIn ` method verifies that the given field's value is ** not**
187
+ The ` whereNotIn ` method verifies that the given field's value is ** not**
186
188
contained in the given array:
187
189
188
190
``` php
@@ -193,7 +195,7 @@ $games = Game::whereNotIn('category', [0,4])->get();
193
195
194
196
###### whereInAll / whereNotInAll / whereInExact / whereNotInExact
195
197
196
- Alternatively you could use one of these methods to match against ** all** or ** exactly** the given array.
198
+ Alternatively you could use one of these methods to match against ** all** or ** exactly** the given array.
197
199
198
200
###### whereNull
199
201
@@ -227,8 +229,7 @@ $games = Game::whereDate('first_release_date', '2019-01-01')->get();
227
229
228
230
###### whereYear
229
231
230
- The ` whereYear ` method may be used to compare a fields's value against a specific
231
- year:
232
+ The ` whereYear ` method may be used to compare a fields's value against a specific year:
232
233
233
234
``` php
234
235
use MarcReichel\IGDBLaravel\Models\Game;
@@ -238,8 +239,7 @@ $games = Game::whereYear('first_release_date', 2019)->get();
238
239
239
240
###### whereHas / whereHasNot
240
241
241
- These methods have the same syntax as ` whereNull ` and ` whereNotNull ` and literally
242
- do the exact same thing.
242
+ These methods have the same syntax as ` whereNull ` and ` whereNotNull ` and literally do the exact same thing.
243
243
244
244
##### Parameter Grouping
245
245
@@ -253,13 +253,13 @@ $games = Game::where('name', 'Fortnite')
253
253
})->get();
254
254
```
255
255
256
- #### Ordering, Limit, & Offset
256
+ #### Ordering, Limit & Offset
257
257
258
258
##### orderBy
259
259
260
- The ` orderBy ` method allows you to sort the result of the query by a given field.
261
- The first argument to the ` orderBy ` method should be the field you wish to sort
262
- by, while the second argument controls the direction of the sort and may be either
260
+ The ` orderBy ` method allows you to sort the result of the query by a given field. The first argument to the ` orderBy `
261
+ method should be the field you wish to sort by, while the second argument controls the direction of the sort and may be
262
+ either
263
263
` asc ` or ` desc ` :
264
264
265
265
``` php
@@ -270,8 +270,8 @@ $games = Game::orderBy('first_release_date', 'asc')->get();
270
270
271
271
##### skip / take
272
272
273
- To limit the number of results returned from the query, or to skip a given
274
- number of results in the query, you may use the ` skip ` and ` take ` methods (` take ` is limited to a maximum of 500):
273
+ To limit the number of results returned from the query, or to skip a given number of results in the query, you may use
274
+ the ` skip ` and ` take ` methods (` take ` is limited to a maximum of 500):
275
275
276
276
``` php
277
277
use MarcReichel\IGDBLaravel\Models\Game;
@@ -289,8 +289,7 @@ $games = Game::offset(10)->limit(5)->get();
289
289
290
290
#### Cache
291
291
292
- You can overwrite the default cache time for one specific query. So you can for
293
- example turn off caching for a query:
292
+ You can overwrite the default cache time for one specific query. So you can for example turn off caching for a query:
294
293
295
294
``` php
296
295
use MarcReichel\IGDBLaravel\Models\Game;
@@ -331,8 +330,7 @@ $game = Game::first();
331
330
332
331
#### Find
333
332
334
- If you know the Identifier of the model you can simply call the ` find ` -method
335
- with the identifier as a parameter:
333
+ If you know the Identifier of the model you can simply call the ` find ` -method with the identifier as a parameter:
336
334
337
335
``` php
338
336
use MarcReichel\IGDBLaravel\Models\Game;
@@ -342,10 +340,9 @@ $game = Game::find(1905);
342
340
343
341
##### FindOrFail
344
342
345
- ` find ` returns ` null ` if no result were found. If you want to throw an Exception
346
- instead use ` findOrFail ` . This will throw an
347
- ` MarcReichel\IGDBLaravel\Exceptions\ModelNotFoundException ` if no result were
348
- found.
343
+ ` find ` returns ` null ` if no result were found. If you want to throw an Exception instead use ` findOrFail ` . This will
344
+ throw an
345
+ ` MarcReichel\IGDBLaravel\Exceptions\ModelNotFoundException ` if no result were found.
349
346
350
347
#### Relationships (Extends)
351
348
@@ -357,9 +354,8 @@ use MarcReichel\IGDBLaravel\Models\Game;
357
354
$game = Game::with(['cover', 'artworks'])->get();
358
355
```
359
356
360
- By default, every field (` * ` ) of the relationship is selected.
361
- If you want to define the fields of the relationship yourself you have to define
362
- the relationship as the array-key and the fields as an array:
357
+ By default, every field (` * ` ) of the relationship is selected. If you want to define the fields of the relationship
358
+ yourself you have to define the relationship as the array-key and the fields as an array:
363
359
364
360
``` php
365
361
use MarcReichel\IGDBLaravel\Models\Game;
@@ -397,8 +393,7 @@ if ($game) {
397
393
398
394
#### Query-Builder-based approach
399
395
400
- If you used the Query Builder itself you must check if a property exists
401
- yourself.
396
+ If you used the Query Builder itself you must check if a property exists yourself.
402
397
403
398
## ✨ Webhooks (since v2.3.0)
404
399
@@ -408,15 +403,28 @@ Since version 2.3.0 of this package you can create webhooks and handle their req
408
403
409
404
#### Configuration
410
405
411
- Inside your ` config/igdb.php ` file you need to have a ` webhook_secret ` of your choice like so (you only need to create this if you upgraded from a prior version of this package. New installations have this configured automatically):
406
+ Inside your ` config/igdb.php ` file you need to have a ` webhook_path ` and ` webhook_secret ` of your choice like so (you
407
+ only need to create this if you upgraded from a prior version of this package. New installations have this configured
408
+ automatically):
412
409
413
410
``` php
414
411
return [
415
412
// ...
416
413
// Other configs
417
414
// ...
418
-
419
- 'webhook_secret' => env('IGDB_WEBHOOK_SECRET', null)
415
+
416
+ /*
417
+ * Path where the webhooks should be handled.
418
+ */
419
+ 'webhook_path' => 'igdb-webhook/handle',
420
+
421
+ /*
422
+ * The webhook secret.
423
+ *
424
+ * This needs to be a string of your choice in order to use the webhook
425
+ * functionality.
426
+ */
427
+ 'webhook_secret' => env('IGDB_WEBHOOK_SECRET', null),
420
428
];
421
429
```
422
430
@@ -426,35 +434,6 @@ And then set a secret inside your `.env` file:
426
434
IGDB_WEBHOOK_SECRET=yoursecret
427
435
```
428
436
429
- #### Routing
430
-
431
- Create a POST route where you want to handle the incoming webhook requests and simply call ` Webhook::handle($request) ` :
432
-
433
- ``` php
434
- use Illuminate\Http\Request;
435
- use MarcReichel\IGDBLaravel\Models\Webhook;
436
-
437
- Route::post('webhook/handle', function (Request $request) {
438
- return Webhook::handle($request);
439
- })->name('handle-webhook');
440
- ```
441
-
442
- Add the route to the ` VerifyCsrfToken ` middleware (in ` app/Http/Middleware/VerifyCsrfToken.php ` ):
443
-
444
- ``` diff
445
- class VerifyCsrfToken extends Middleware
446
- {
447
- /**
448
- * The URIs that should be excluded from CSRF verification.
449
- *
450
- * @var array
451
- */
452
- protected $except = [
453
- + 'webhook/handle'
454
- ];
455
- }
456
- ```
457
-
458
437
That's it!
459
438
460
439
### Creating a webhook
@@ -463,25 +442,28 @@ Let's say we want to be informed whenever a new game is created on https://igdb.
463
442
464
443
First of all we need to inform IGDB that we want to be informed.
465
444
466
- For this we create a webhook like so:
445
+ For this we create a webhook like so (for example inside a controller) :
467
446
468
447
``` php
448
+ use MarcReichel\IGDBLaravel\Enums\Webhook\Method;
469
449
use MarcReichel\IGDBLaravel\Models\Game;
450
+ use Illuminate\Routing\Controller;
470
451
471
- Game::createWebhook(route('handle-webhook'), 'create');
452
+ class ExampleController extends Controller
453
+ {
454
+ public function createWebhook()
455
+ {
456
+ Game::createWebhook(Method::CREATE)
457
+ }
458
+ }
472
459
```
473
460
474
- The first parameter describes the route where we want to handle the webhook.
475
- The second parameter needs to be one of ` create ` , ` update ` or ` delete ` according to
476
- which event we want to listen for.
477
-
478
461
### Listen for events
479
462
480
- Now that we have created our webhook we can listen for a specific event - in our case
481
- when a game is created.
463
+ Now that we have created our webhook we can listen for a specific event - in our case when a game is created.
482
464
483
- For this we create a Laravel EventListener or for sake of simplicity we just listen for an event
484
- inside the ` boot() ` method of our ` app/providers/EventServiceProvider.php ` :
465
+ For this we create a Laravel EventListener or for sake of simplicity we just listen for an event inside the ` boot() `
466
+ method of our ` app/providers/EventServiceProvider.php ` :
485
467
486
468
``` php
487
469
use MarcReichel\IGDBLaravel\Events\GameCreated;
@@ -490,14 +472,52 @@ use Illuminate\Support\Facades\Event;
490
472
public function boot()
491
473
{
492
474
Event::listen(function (GameCreated $event) {
493
- // $event->game holds the game data
475
+ // $event->data holds the (unexpanded!) data (of the game in this case)
494
476
});
495
477
}
496
478
```
497
479
498
480
[ Here] ( src/Events ) you can find a list of all available events.
499
481
500
- Further information on how to set up event listeners can be found on the [ official docs] ( https://laravel.com/docs/events ) .
482
+ Further information on how to set up event listeners can be found on
483
+ the [ official docs] ( https://laravel.com/docs/events ) .
484
+
485
+ ### Manage webhooks via CLI
486
+
487
+ #### List your webhooks
488
+
489
+ ``` bash
490
+ $ php artisan igdb:webhooks
491
+ ```
492
+
493
+ #### Create a webhook
494
+
495
+ ``` bash
496
+ $ php artisan igdb:webhooks:create {model? } {--method=}
497
+ ```
498
+
499
+ You can also just call ` php artisan igdb:webhooks:create ` without any arguments. The command will then ask for the
500
+ required data interactively.
501
+
502
+ The ` model ` parameter needs to be the (studly cased) class name of a model (e.g. ` Game ` ).
503
+
504
+ The ` --method ` option needs to be one of ` create ` , ` update ` or ` delete ` accordingly for which event you want to listen.
505
+
506
+ #### Reactivate a webhook
507
+
508
+ ``` bash
509
+ $ php artisan igdb:webhooks:reactivate {id}
510
+ ```
511
+
512
+ For ` {id} ` insert the id of the (inactive) webhook.
513
+
514
+ #### Delete a webhook
515
+
516
+ ``` bash
517
+ $ php artisan igdb:webhooks:delete {id? } {--A| all}
518
+ ```
519
+
520
+ You may provide the ` id ` of a webhook to delete it or use the ` -A ` /` --all ` flag to delete all your registered webhooks.
501
521
502
522
## Testing
503
523
0 commit comments