Download the PHP package rasyidly/laravel-model-query without Composer
On this page you can find all versions of the php package rasyidly/laravel-model-query. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rasyidly/laravel-model-query
More information about rasyidly/laravel-model-query
Files in rasyidly/laravel-model-query
Package laravel-model-query
Short Description Advanced laravel dynamic model queries
License MIT
Informations about the package laravel-model-query
Laravel Model Query
Advanced laravel dynamic model queries
Installation
Install this package with composer
Usage
There are several features including this package. To use all features, use this as trait as following example.
OR, manually use single feature as trait
Just use feature/s as a trait to your Model, e.g.:
Features
There are main feature of this package features and how-to-use.
📃 Loadable
Loadable is a trait that functions to load eloquent relations based on the $loadable property defined in the Model, like this:
If the relationship is not found in the definition, it will not give an error. Unless you give the wrong definition of the relationship, it is fatal.
Then, for use in the Controller, as follows:
Eager loading
This will produce a collection/object of Post with the relations author and commentable by eager loading. Make sure the query is separated with comma if load more than one relation, so that loadable accepts array parameters according to Loadable's terms.
Lazy loading
It's the same, the only difference is that it will be loaded when it becomes an object (not a query builder a.k.a. n+1)
This will load the author.profile relation as per the $loadable definition.
📃 Searchable
Searchable is a trait that functions to search for specific text based on the $searchable property defined in the Model, like this:
With this searchable definition, in sequence, it will search for a collection of Posts where title like % ... %, and so on. You can also use a relationship like the example above, author.name, meaning it will look for name in the author relationship
Then, for use in the Controller, as follows:
The result of the syntax above is to search for posts in the column defined by $searchable, in this case it will search like "%Hello%" in the title or description column or name in the author relationship
📃 Sortable
The sortable feature is a method for sorting by columns defined in the $sortable property, with the addition of ,asc or ,desc.
An example of implementation in the Controller is as follows:
Basically, sortable is exactly like Laravel's built-in orderBy method, only I combine the sorting method, whether ascending or descending. This trait only accepts two ascending or descending parameters, otherwise it will ignore this method, no error will be displayed.
📃 Trashable
This trait only works if the Model uses SoftDeletes
There are no additional special properties like in the previous trait. This will only add a special method namely trashable($default = 'without') which only accepts 3 parameters namely with, only, and without.
The implementation is as follows:
The result of the query and syntax is that it will only call onlyTrashed from Laravel SoftDeletes.
Contributing
Contributions are always welcome, just open issues, fork and/or pull your request! Up to you!
License
This library is open-source software licensed under the MIT