TOPIC COLLECTION
API.
A collection of 9 practical ideas to make everyday Laravel work a little smoother.
09 TIPS01API Resources: with or without "Data"?If you use Eloquent API Resources to return data, they will be automatically wrapped in 'data'. If you want to remove it, add…API02Conditional Relationship Counts on API ResourcesYou may conditionally include the count of a relationship in your resource response by using the whenCounted method. By doing so, the attribute is not…API03API Return "Everything Went Ok"If you have API endpoint which performs some operations but has no response, so you wanna return just "everything went ok", you may return 204 status code…API04Avoid N+1 Queries in API ResourcesYou can avoid N+1 queries in API resources by using the whenLoaded() method. This will only append the department if it’s already loaded in the Employee model.API05Get Bearer Token from Authorization HeaderThe bearerToken() function is very handy when you are working with apis & want to access the token from Authorization header.API06Sorting Your API ResultsSingle-column API sorting, with direction control we do the same for multiple columns (e.g., ?sort=name,-weight)API07Customize Exception Handler for APILaravel 8 and below: There's a method render() in App\Exceptions classAPI08Force JSON Response for API RequestsIf you have built an API and it encounters an error when the request does not contain "Accept: application/JSON " HTTP Header then the error will be…API09API VersioningWhen to version? If you are working on a project that may have multi-release in the future or your endpoints have a breaking change like a change in the…API