You can specify custom includes using the AllowedInclude::custom() method. Using aliases you can specify a new, shorter name for this include: use Spatie\QueryBuilder\AllowedInclude // GET /users?include=profile $users = QueryBuilder :: for ( User ::class ) -> allowedIncludes ( AllowedInclude :: relationship ( 'profile', 'userProfile' )) // will include the `userProfile` relationship -> get () #Custom includes ![]() For example, your users table might have a userProfile relationship, which might be neater just specified as profile. It can be useful to specify an alias for an include to enable friendly relationship names. GET /users?include=postsExists,friendsExists $users = QueryBuilder :: for ( User ::class ) -> allowedIncludes () // every user in $users will contain a `posts_exists` and `friends_exists` property #Include aliases Read more about the withExists method here. Under the hood this uses Laravel's withExists method. On top of that it's also possible to specifically allow requesting and querying the related model exists (and not include the entire relationship). GET /users?include=postsCount,friendsCount $users = QueryBuilder :: for ( User ::class ) -> allowedIncludes () // every user in $users will contain a `posts_count` and `friends_count` property #Including related model existsĮvery allowed include will automatically allow requesting its related model exists using a Exists suffix. Read more about the withCount method here. Under the hood this uses Laravel's withCount method. On top of that it's also possible to specifically allow requesting and querying the related model count (and not include the entire relationship). notation: // GET /users?include=ments,permissions $users = QueryBuilder :: for ( User ::class ) -> allowedIncludes () -> get () // $users will contain all users with their posts, comments on their posts and permissions loaded #Including related model countĮvery allowed include will automatically allow requesting its related model count using a Count suffix. ![]() You can load nested relationships using the dot. Its exception message contains the allowed includes for reference. When trying to include relationships that have not been allowed using allowedIncludes() an InvalidIncludeQuery exception will be thrown. Default relationships are built-in to Laravel itself using the with() method on a query: $users = QueryBuilder :: for ( User ::class ) -> allowedIncludes () -> with ( 'posts' ) // posts will always by included, friends can be requested -> withCount ( 'posts' ) -> withExists ( 'posts' ) -> get () #Disallowed includes There is no way to include relationships by default in this package. ![]() You can load multiple relationships by separating them with a comma: // GET /users?include=posts,permissions $users = QueryBuilder :: for ( User ::class ) -> allowedIncludes () -> get () // $users will contain all users with their posts and permissions loaded #Default includes #Basic usage // GET /users?include=posts $users = QueryBuilder :: for ( User ::class ) -> allowedIncludes () -> get () // $users will have all their their `posts()` related models loaded This method takes an array of relationship names or AllowedInclude instances. For example, a blog post may have many comments, or an order could be related to the user who placed it.The include query parameter will load any Eloquent relation or relation count on the resulting models.Īll includes must be explicitly allowed using allowedIncludes(). Dynamic Propertiesĭatabase tables are often related to one another. Defining Custom Intermediate Table Models.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |