Skip to main content

LoadRelatives

Trait LoadRelatives 

Source
pub trait LoadRelatives<M> {
    // Required methods
    fn with_ancestors(
        self,
    ) -> impl Future<Output = Result<Vec<WithAncestors<M>>>> + Send;
    fn with_descendants(
        self,
    ) -> impl Future<Output = Result<Vec<TreeNode<M>>>> + Send;
    fn with_subtree_counts<R, T>(
        self,
        foreign_key: Column<R, T>,
    ) -> impl Future<Output = Result<Vec<WithCount<M>>>> + Send
       where R: Model,
             T: Send + Sync + 'static;
    fn with_subtree_counts_where<R, T>(
        self,
        foreign_key: Column<R, T>,
        filter: impl IntoCondition + Send,
    ) -> impl Future<Output = Result<Vec<WithCount<M>>>> + Send
       where R: Model,
             T: Send + Sync + 'static;
}
Expand description

Loads the relatives of every node in a list with one query, instead of one per node.

use laravel_nestedset::LoadRelatives;
// Breadcrumbs for a page of search results: two queries, however long the page.
let results = Category::query()
    .filter(Category::name.contains(term))
    .limit(20)
    .get()
    .await?
    .with_ancestors()
    .await?;

for result in &results {
    let path: Vec<&str> = result.ancestors.iter().map(|node| node.name.as_str()).collect();
}

Required Methods§

Source

fn with_ancestors( self, ) -> impl Future<Output = Result<Vec<WithAncestors<M>>>> + Send

Each node with the nodes above it, from the root down.

§Errors

Fails when the query fails.

Source

fn with_descendants( self, ) -> impl Future<Output = Result<Vec<TreeNode<M>>>> + Send

Each node as the top of its own subtree, nested: the menu below each of several roots, for example.

§Errors

Fails when the query fails.

Source

fn with_subtree_counts<R, T>( self, foreign_key: Column<R, T>, ) -> impl Future<Output = Result<Vec<WithCount<M>>>> + Send
where R: Model, T: Send + Sync + 'static,

Each node with a count of the rows of another table that point at it or at any node below it: the products of a category and all its subcategories, say. One query for the list:

use laravel_nestedset::LoadRelatives;

let menu = Category::roots()
    .get()
    .await?
    .with_subtree_counts(Product::category_id)
    .await?;

for entry in &menu {
    println!("{} ({})", entry.node.name, entry.count); // Phones (124)
}
§Errors

Fails when the query fails.

Source

fn with_subtree_counts_where<R, T>( self, foreign_key: Column<R, T>, filter: impl IntoCondition + Send, ) -> impl Future<Output = Result<Vec<WithCount<M>>>> + Send
where R: Model, T: Send + Sync + 'static,

with_subtree_counts, counting only the rows that filter matches, such as Product::published.eq(true). The filter may use the other table’s typed columns: the query joins that table under its own name.

§Errors

Fails when the query fails.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<M: NestedSet> LoadRelatives<M> for Vec<M>

Source§

async fn with_subtree_counts<R, T>( self, foreign_key: Column<R, T>, ) -> Result<Vec<WithCount<M>>>
where R: Model, T: Send + Sync + 'static,

Source§

async fn with_subtree_counts_where<R, T>( self, foreign_key: Column<R, T>, filter: impl IntoCondition + Send, ) -> Result<Vec<WithCount<M>>>
where R: Model, T: Send + Sync + 'static,

Source§

async fn with_ancestors(self) -> Result<Vec<WithAncestors<M>>>

Source§

async fn with_descendants(self) -> Result<Vec<TreeNode<M>>>

Implementors§