Created Models
The created models will have methods that help you control the flow of your application
searchableArray method
the defined searchableArray method will power your index controllers with the ability to search inside the returned columns on within the requested query but this is a limitation to the usage of the globalQuery method in your model corresponding repository . check on the allWithPagination method in the base repository then its call from the base service class .
this mean querying your models like this :
class PostRepository extends BaseRepository
{
public function index(){
return $this->globalQuery()->where('likes' , '>' , 50)->paginate(10);
}
}will give you the ability to use a query param in your request named search with the value of the keyword you want to search and the query will search for it in the defined columns ,so if we have this columns :
public function searchableArray() : array
{
return [
'title' ,
'body'
];
}it will search just within the title and body columns .
relationsSearchableArray method
the same things in the searchableArray method applied here just instead in searching in the table columns it searches in the related tables columns and its definition looks like this :
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
public function author()
{
return $this->belongsTo(Author::class);
}
public function relationsSearchableArray()
{
return [
'comments' => [
'body'
] ,
'author' => [
'name'
]
];
}
}this mean you could search using the search query param inside the body column of posts comments and in the posts author names.
filterArray method
we will not generate this method in your models , so you have to add it manually as it is very customizable but in basic it powers up your globalQuery with the ability to filter the returned values .
So basically this method will return a nested array like the following :
public function filterArray() : array
{
return [
[
'field' => 'views', // this key could also be name so : 'name' => 'likes'
'operator' => '>=', // the default value is '=' , it could also set to 'like' operator or whatever where clause operator
'method' => 'where', // it is a query so set the method you like it to filter : 'whereColumn' , default value is 'where'
'query' => null, // here you can provide a call back function that takes an instance of the global query implemented within your query to do whatever you want
],
[
'name' => 'likes', // the relation column to filter
'relation' => 'comments' , // the relation we want to filter the query depending on it
],
]
}so basically after having the previous filterArray method we gain the ability to send query params named views or likes with values represent the desired data for example and based on the defined method :
- sending a query param within the request named
viewswith the value of10while querying using theglobalQuerywill return all the posts which have views more than10. - sending a query param within the request named
likeswith the value of10while querying using theglobalQuerywill return all the posts which have comments with likes equals to10.
providing the query params values as arrays that represent a range will ignore the defined method and use the whereBetween method to query your data where the first array element and the second one fits in the whereBetween method.
ordering results
When querying a model using its repository globalQuery method you can order the results based on each column in the corresponding table by using 2 query params :
sort_colas the name of the column you want to order the results based on it.sort_diras the order directionasc,desc.
so when your request contain those params and your request uses the globalQuery method it will return the response data ordered by the selected column .
WARNING
the functionality of the previous methods depends on using the globalQuery method based in the BaseRepository class read more about it here
File columns (MediaCast & HasMedia)
File columns are handled through the MediaCast cast and the HasMedia trait, both added to the model automatically when it has a file column — you don't declare a list of file keys by hand.
Any column cast to \App\Casts\MediaCast::class is treated as a file. So when you create or update a record through the repository's create / update methods and pass an uploaded file for that column, the file is stored and its path is saved in the column. The HasMedia trait also hooks into the model's deleted event to remove the stored files when a record is deleted.
protected function casts(): array
{
return [
'image' => \App\Casts\MediaCast::class,
];
}