You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1.0 - Laravel 5.5 - 5.8 - Bug fixes only, mainly there for backward compatibility, if there are any issues then create a pull request.
16
16
17
-
2.0 - Laravel 6.0 - Maintanined, again feel free to create pull requests. This is open source which is a 2 way street.
17
+
2.0 - Laravel 6.0 - Maintained, again feel free to create pull requests. This is open source which is a 2 way street.
18
18
19
-
3.0 - Laravel 7.0 - Maintanined, again feel free to create pull requests. This is open source which is a 2 way street. Reason why it only uses Laravel 7 is because we hook into the new HTTP/Client and some other models in Laravel, thankfully created by Taylor Otwell.
19
+
3.0 - Laravel 7.0 - Maintained, again feel free to create pull requests. This is open source which is a 2 way street.
20
20
21
21
## Usage
22
22
@@ -189,12 +189,14 @@ Xero uses a patch request for creating models and post request for updating mode
189
189
190
190
## Retreiving models
191
191
192
-
You can retrieve a single model by either using the find method and passing an ID, or an array of ID's, or by running a query and returning first();
192
+
You can retrieve a single model by either using the find method and passing an ID, or an array of ID's, or by running a query and returning first() or last();
$user = API::user()->where('Name', 'Bob')->first(); // First occurence
198
+
199
+
$user = API::user()->where('Name', 'Bob')->last(); // Last occurence
198
200
```
199
201
200
202
You can also use get and all to retreive many models, this will return a Result Set, which is an enhanced Laravel Collection.
@@ -317,17 +319,17 @@ We utilise Laravel's hasAttributes trait in the models so you should be able to
317
319
318
320
### Resources
319
321
320
-
These are generally gesources that are returned as relationships of other models but do not interact with the API directly. To create one of these extend the MacsiDigital/API/Support/Resource.
322
+
These are generally gesources that are returned as relationships of other models but do not interact with the API directly. To create one extend the MacsiDigital/API/Support/Resource.
321
323
322
-
THis should only be used on models that are returned as a sub array of a called model.
324
+
This should only be used on models that are returned as a sub array of a called model.
323
325
324
-
### ApiResources
326
+
### API Resources
325
327
326
-
These are models that will interact directly with an API, or indirectly through a parent model. To create one of these extend the MacsiDigital/API/Support/APIResource.
328
+
These are models that will interact directly with an API, or indirectly through a parent model. To create one extend the MacsiDigital/API/Support/APIResource.
327
329
328
330
If you want to roll your own then you need to ensure you add the MacsiDigital\API\Traits\InteractsWithAPI trait. Also follow how our Support API resource works.
329
331
330
-
In these models we also variosu info we store, like primary key and api endpoints, these are the available attributes
332
+
In these models we also house many variables, like primary key and api endpoints, these are the available attributes
331
333
332
334
```php
333
335
// These will map to RESTful requests
@@ -353,7 +355,7 @@ In these models we also variosu info we store, like primary key and api endpoint
353
355
// Also, some API's return 'users' for multiple and user for single, set teh multiple field below to wheat is required if different
354
356
protected $apiMultipleDataField = 'data';
355
357
```
356
-
The apiData and apiMultiple fields dictate what fields that are being returned contains the resources, this should be 'data' in a good api but it really does vary. Zoom uses 'users' for multiple records and '' for single.
358
+
The apiData and apiMultiple fields dictate what field in the response will house the main body data, this should be 'data' in a good api but it really does vary. Zoom uses 'users' for multiple records and '' for single.
357
359
358
360
If apiDataField is set to '' it will return the body direct.
359
361
@@ -363,7 +365,7 @@ If apiDataField is set to '' it will return the body direct.
363
365
364
366
Xero also uses a different method.
365
367
366
-
In the case of Xero it can be overwritten in the getApiDatField function so that we dont have to set on all models.
368
+
In the case of Xero it can be overwritten in the getApiMultipleDataField function so that we dont have to set on all models.
367
369
368
370
```php
369
371
public function getApiMultipleDataField()
@@ -390,9 +392,18 @@ A quick note on endpoints, we can set an endpoint as 'users' but we can also inc
390
392
protected $endPoint = 'users/{user:id}/settings';
391
393
```
392
394
395
+
We can also set customEndpoints on a model for specific endPoints, if the endPoints dont follow convention.
396
+
397
+
```php
398
+
protected $customEndPoints = [
399
+
'get' => 'users/{user:id}/meetings',
400
+
'post' => 'users/{user:id}/meetings'
401
+
];
402
+
```
403
+
393
404
### Relationships
394
405
395
-
We have tried to get the models as close to a Laravel model as we can, even down to how relationships work.
406
+
We have tried to get the models as close to a Laravel model as we can, even down to how relationships work.
396
407
397
408
By default we will try to create relationship objects for any returned input if they are setup. However you can override this behaviour by setting LoadRaw to true in the model
398
409
@@ -424,7 +435,7 @@ This could also be a HasMany relationship and the reverse on the address would b
424
435
}
425
436
```
426
437
427
-
A name and a field can be passed as a 2nd and 3rd argument.
438
+
A name and a field can be passed as a 2nd and 3rd argument. A 4th argument can also passed which will be any fields and values in an array that should be passed onto all relationship models. This is handy when you need to track a parent's id on teh child model.
428
439
429
440
We try to automatically work out the name and field attributes if not passed for you based on the function name, so we will look for a field 'user_id' in the array 'users' in the above method. However not all API's use the same id naming so you can set the IDSuffix by adding this to your model.
430
441
@@ -470,7 +481,7 @@ We utilise save, saveMany (has many only), create and createMany (has many only)
470
481
$user = API::user()->find('id');
471
482
472
483
$user->address()->create([
473
-
'address1' => '21 Test Street',
484
+
'address1' => '17 Test Street',
474
485
...
475
486
]);
476
487
```
@@ -494,7 +505,7 @@ When the relation models do not interact with the api then we expose new make()
494
505
$user->address()->attach($address);
495
506
```
496
507
497
-
This works well when relationships are returned direct as part of an API call, which is common in API's. However sometimes API's dont send these items direct adn therefore need different end point calls.
508
+
This works well when relationships are returned direct as part of an API call, which is common in API's. However sometimes API's dont send these items direct and therefore need different end point calls.
498
509
499
510
In these cases we use custom relationship models
500
511
@@ -504,7 +515,7 @@ First you need to extend either the HasOne or HasMany relationship models.
504
515
505
516
Within the extended model you need to create the logic for retreiving, creating, updating and deleting as is required by the API. Unfortunetly as these are all case specific you will need to create CustomRelations models for any implementation required.
506
517
507
-
To call it we call the hasCustom method and pass the model class as the 2nd parameter. Parameters 3 and 4 can still be the name and field.
518
+
To call it we call the hasCustom method and pass the model class as the 2nd parameter. Parameters 3 and 4 can still be the name and field and 5 any fields and values to add to any new models.
508
519
509
520
```php
510
521
public function addresses()
@@ -605,7 +616,7 @@ As arrays cant use symbols as keys we do some translating, so if '=' is called w
605
616
606
617
The default is called for any custom filters, so lets say you call where('name', 'StartsWith', 'Bob') this will call addWhereStartsWith and processWhereStartsWith methods
607
618
608
-
Now each API will handle filtering differently so the logic in these methods are to suit the API, here is an example for xero, who add all filters to a where query string. They allow the main ones and have 3 custom types, 'Contains' which is the same as a 'like' call, 'StartWith' and 'EndsWith'.
619
+
Now each API will handle filtering differently so the logic in these methods are to suit the API, here is an example for Xero, who add all filters to a where query string. They allow the main ones and have 3 custom types, 'Contains' which is the same as a 'like' call, 'StartWith' and 'EndsWith'.
609
620
610
621
```php
611
622
public function processEquals($detail)
@@ -720,7 +731,7 @@ Of course for these sorts of custom actions we have to update the allowed operan
720
731
721
732
## Creating and Updating
722
733
723
-
Generally in API's the attributes required for saving and updating are different, and they are in turn differnet to the attributes when a model is retreived. We therefore utilise 'Persistance' models which will house validation logic and the attributes required to make a successful save. So in our models we need to Extend the InteractsWithAPI trait and add the following 2 protected attributes.
734
+
Generally in API's the attributes required for saving and updating are different, and they are in turn different to the attributes when a model is retrieved. We therefore utilise 'Persistance' models which will house validation logic and the attributes required to make a successful save. So in our models we need to Extend the InteractsWithAPI trait and add the following 2 protected attributes.
@@ -846,7 +857,7 @@ To save its as simple as calling the save() function.
846
857
$user->save();
847
858
```
848
859
849
-
THe model will see if it already exists and call the correct update method. If updating we will only pass dirty attributes to the persistance model.
860
+
THe model will see if it already exists and call the correct insert or update method. If updating we will only pass dirty attributes to the persistance model.
850
861
851
862
You can also utilise other laravel methods like make, create and update directly in the model.
0 commit comments