Expand description
Search through several models at once, and get one ordered and paginated list of mixed models.
This is Cross Model Search for Laravel Rust. Declare an enum with a variant for each model, add each model’s query and the columns to search through, and run the search:
use cross_model_search::{SearchResult, through};
use serde::Serialize;
#[derive(SearchResult, Serialize)]
#[serde(tag = "type")]
enum Hit {
Post(Post),
Video(Video),
}
let hits = Hit::search("laravel eloquent")
.add_query(Post::query().with(Post::comments), (Post::title, through(Post::comments, Comment::body)))
.add(Video::title)
.order_by_desc()
.paginate(15)
.await?;
for hit in hits.items() {
match hit {
Hit::Post(post) => println!("post {}", post.title),
Hit::Video(video) => println!("video {}", video.title),
}
}The documentation in docs/ covers each feature in Laravel PHP and Laravel Rust.
Structs§
- Key
- The key of a model of one of a
SearchResulttype’s variants, read from a search’s combined rows. - Missing
Model Error - A model that the search found was gone when the search loaded it, because another request deleted or changed it between the two queries.
- NoModels
Error - A search needs at least one model to search through.
- Paginated
Search - A
Searchthat returns a page of results with aPaginator. - Search
- A search through several models at once, which returns one list of
H, ordered and optionally paginated. - Search
Column - A column that a search looks through for a model
M. - Simple
Paginated Search - A
Searchthat returns a page of results with aSimplePaginator.
Traits§
- From
Model - A
SearchResulttype that holds models of typeMin the variant numberedVARIANT. - Into
Columns - One or more columns that a search looks through for the model
M. - Model
Order - A tuple of models, in the order that
Search::order_by_modelorders the results by. - Search
Result - A type that holds one search result, which is a model of one of several types.
Functions§
- parse_
terms - Split
termsinto the words to search for. - through
- Search
columnsof the models thatrelationrelates to, such as the bodies of a post’s comments withthrough(Post::comments, Comment::body).
Derive Macros§
- Search
Result - Derive
SearchResult,FromModel, and asearchfunction on an enum whose variants each hold one model.