Skip to main content

Column

Struct Column 

Source
pub struct Column<M, T> { /* private fields */ }
Expand description

A column of model M whose values have type T, such as User::email.

#[model] adds a column for each field. A column works wherever a query takes a column, such as order_by, add_select, and pluck, and the query qualifies it with the model’s table. Its comparisons only accept values of the column’s type:

use laravel::model::model;
use laravel::time::Timestamp;

#[model]
pub struct Post {
    pub id: i64,
    pub title: String,
    pub votes: i64,
    pub published_at: Option<Timestamp>,
    pub created_at: Timestamp,
    pub updated_at: Timestamp,
}

let posts = Post::query()
    .filter(Post::title.starts_with("Laravel") & Post::votes.ge(10))
    .filter(!Post::published_at.is_null() & Post::updated_at.gt(Post::created_at))
    .order_by(Post::votes.desc())
    .get()
    .await?;

Comparing with a value of another type doesn’t compile:

ⓘ
let condition = User::id.eq("x");

A comparison or is_in also accepts a single-column subquery, such as a model query narrowed with Builder::select_column or a db::table query:

let active = User::query().filter(User::active.eq(true)).select_column(User::id);
let average = Post::query().select_column(Post::votes.avg());

let posts = Post::query()
    .filter(Post::user_id.is_in(active) & Post::votes.gt(average))
    .get()
    .await?;

A model query selects every column, so it isn’t a subquery:

ⓘ
let condition = User::id.is_in(User::query());

A list of another type doesn’t compile either:

ⓘ
let condition = User::id.is_in(["x"]);

Only a column of an Option type has is_null:

ⓘ
let condition = User::name.is_null();

Only a text column has like, starts_with, ends_with, and contains:

ⓘ
let condition = User::votes.contains("1");

Implementations§

Source§

impl<M, T> Column<M, T>

Source

pub const fn name(&self) -> &'static str

Returns the column’s name, without the table.

Source§

impl<M, T> Column<M, T>

Source

pub fn eq(self, value: impl ComparableTo<T>) -> Condition
where T: Send + Sync + 'static,

Matches rows where the column equals value.

On a column of an Option type, eq(None) matches rows where the column is NULL.

Source

pub fn ne(self, value: impl ComparableTo<T>) -> Condition

Matches rows where the column doesn’t equal value.

On a column of an Option type, ne(None) matches rows where the column isn’t NULL.

Source

pub fn lt(self, value: impl ComparableTo<T>) -> Condition

Matches rows where the column is less than value.

Source

pub fn le(self, value: impl ComparableTo<T>) -> Condition

Matches rows where the column is less than or equal to value.

Source

pub fn gt(self, value: impl ComparableTo<T>) -> Condition

Matches rows where the column is greater than value.

Source

pub fn ge(self, value: impl ComparableTo<T>) -> Condition

Matches rows where the column is greater than or equal to value.

Source

pub fn is_in(self, values: impl ComparableList<T>) -> Condition

Matches rows where the column equals one of values, or one of the rows of a db::table subquery. An empty list matches no rows.

Source

pub fn is_not_in(self, values: impl ComparableList<T>) -> Condition

Matches rows where the column doesn’t equal any of values, or any of the rows of a db::table subquery. An empty list matches every row.

Source

pub fn between( self, low: impl ComparableTo<T>, high: impl ComparableTo<T>, ) -> Condition

Matches rows where the column is between low and high, including both ends.

Source

pub fn asc(self) -> Order
where T: Decode,

Orders by this column, smallest first.

Source

pub fn desc(self) -> Order
where T: Decode,

Orders by this column, largest first.

Source

pub fn expr(self) -> Expr

Returns the column as an expression qualified with its table, such as "users"."email".

Source

pub fn count(self) -> Expr

Counts rows with a non-null value in this column (COUNT(column)).

Source

pub fn count_distinct(self) -> Expr

Counts distinct non-null values in this column (COUNT(DISTINCT column)).

Source

pub fn sum(self) -> Expr

Sums the values of this column (SUM(column)).

Source

pub fn avg(self) -> Expr

Averages the values of this column (AVG(column)).

Source

pub fn min(self) -> Expr

Returns the smallest value of this column (MIN(column)).

Source

pub fn max(self) -> Expr

Returns the largest value of this column (MAX(column)).

Source§

impl<M, T> Column<M, Option<T>>

Source

pub fn is_null(self) -> Condition

Matches rows where the column is NULL. Use is_not_null or negate the condition with ! to match rows where the column isn’t NULL.

Source

pub fn is_not_null(self) -> Condition

Matches rows where the column isn’t NULL.

Source§

impl<M, T> Column<M, T>

Conditions that compare a Timestamp or Option<Timestamp> column with laravel::now().

Each method reads the application’s clock when you call it, so a test that travels in time changes the result. A NULL value meets neither a condition nor its negation.

use laravel::model::model;
use laravel::time::Timestamp;

#[model]
pub struct Invoice {
    pub id: i64,
    pub paid_at: Option<Timestamp>,
    pub due_at: Timestamp,
}

let overdue = Invoice::query()
    .filter(Invoice::due_at.is_past() & Invoice::paid_at.is_null())
    .get()
    .await?;

let due_today = Invoice::query().filter(Invoice::due_at.is_today()).get().await?;

let due_later = Invoice::query()
    .filter(!Invoice::due_at.is_before_today())
    .get()
    .await?;

A column of another type doesn’t have these methods:

ⓘ
let condition = User::name.is_past();
Source

pub fn is_past(self) -> Condition

Matches rows where the column is earlier than now. Negate the condition with ! to match now or later.

The condition is the same as Expr::is_past.

Source

pub fn is_future(self) -> Condition

Matches rows where the column is later than now. Negate the condition with ! to match now or earlier.

The condition is the same as Expr::is_future.

Source

pub fn is_today(self) -> Condition

Matches rows where the column falls on today’s date in UTC.

The condition is the same as Expr::is_today.

Source

pub fn is_before_today(self) -> Condition

Matches rows where the column falls on a date before today in UTC. Negate the condition with ! to match today or later.

Source

pub fn is_after_today(self) -> Condition

Matches rows where the column falls on a date after today in UTC. Negate the condition with ! to match today or earlier.

Source§

impl<M, T> Column<M, T>
where T: __Text,

Source

pub fn like(self, pattern: impl AsRef<str>) -> Condition

Matches rows where the column matches pattern, in which % matches any text and _ matches one character.

PostgreSQL ignores case. MySQL and SQLite also ignore case with their default collations, but SQLite only ignores the case of ASCII letters.

Source

pub fn like_case_sensitive(self, pattern: impl AsRef<str>) -> Condition

Matches rows where the column matches pattern, treating upper and lower case as different on every database.

The condition is the same as Expr::like_case_sensitive.

Source

pub fn starts_with(self, text: impl AsRef<str>) -> Condition

Matches rows where the column starts with text.

The % and _ characters in text match only themselves. Each database ignores case the same way it does for Self::like.

Source

pub fn ends_with(self, text: impl AsRef<str>) -> Condition

Matches rows where the column ends with text.

As with Self::starts_with, the % and _ characters in text match only themselves.

Source

pub fn contains(self, text: impl AsRef<str>) -> Condition

Matches rows where the column contains text.

As with Self::starts_with, the % and _ characters in text match only themselves.

Trait Implementations§

Source§

impl<M, T> Clone for Column<M, T>

Source§

fn clone(&self) -> Column<M, T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<N, T> ComparableTo<Option<T>> for Column<N, T>

Source§

impl<N, T> ComparableTo<T> for Column<N, T>

Source§

impl<N, T> ComparableTo<T> for Column<N, Option<T>>

Source§

impl<M, T> Copy for Column<M, T>

Source§

impl<M, T> Debug for Column<M, T>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<M, T> From<Column<M, T>> for Expr

Source§

fn from(column: Column<M, T>) -> Expr

Converts to this type from the input type.
Source§

impl<M, T> From<Column<M, T>> for Order
where T: Decode,

Source§

fn from(column: Column<M, T>) -> Order

Converts to this type from the input type.
Source§

impl<M, T> From<Column<M, T>> for Selection

Source§

fn from(column: Column<M, T>) -> Selection

Converts to this type from the input type.
Source§

impl<M, T, V> InOrderOfColumn<V> for Column<M, T>
where V: ComparableTo<T>,

Source§

impl<M, T> IntoColumn for Column<M, T>

Source§

fn into_column(self) -> Expr

Convert the value into a column expression.
Source§

impl<M, T> IntoExpr for Column<M, T>

Source§

fn into_expr(self) -> Expr

Convert the value into an expression.
Source§

impl<M, C, T> OrderColumn<M> for Column<C, T>
where M: Model, C: QueryTable<M>, T: Decode,

Auto Trait Implementations§

§

impl<M, T> Forget for Column<M, T>

§

impl<M, T> Freeze for Column<M, T>
where &'static str: Freeze, PhantomData<fn() -> (M, T)>: Freeze,

§

impl<M, T> Move for Column<M, T>

§

impl<M, T> RefUnwindSafe for Column<M, T>

§

impl<M, T> Send for Column<M, T>
where &'static str: Send,

§

impl<M, T> Sync for Column<M, T>

§

impl<M, T> Unpin for Column<M, T>
where &'static str: Unpin,

§

impl<M, T> UnsafeUnpin for Column<M, T>
where &'static str: UnsafeUnpin, PhantomData<fn() -> (M, T)>: UnsafeUnpin,

§

impl<M, T> UnwindSafe for Column<M, T>
where &'static str: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsOut<T> for T
where T: Copy,

Source§

fn as_out(&mut self) -> Out<'_, T>

Returns an out reference to self.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Fake for T

Source§

fn fake<U>(&self) -> U
where Self: FakeBase<U>,

Source§

fn fake_with_rng<U, R>(&self, rng: &mut R) -> U
where R: RngExt + ?Sized, Self: FakeBase<U>,

Source§

impl<T> FillValue<Option<T>> for T
where T: Send + 'static,

Source§

async fn fill(self) -> Result<Option<T>, Error>

Resolves the column’s value. Read more
Source§

impl<T> FillValue<T> for T
where T: Send + 'static,

Source§

async fn fill(self) -> Result<T, Error>

Resolves the column’s value. Read more
Source§

impl<K> ForeignKey<K> for K

Source§

fn key(&self) -> Option<&K>

Returns the key, or None when the field holds no key.
Source§

fn __from_key(key: K) -> K

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoJoinValue<Option<T>> for T
where T: IntoExpr,

Source§

fn __value(self) -> Expr

Source§

impl<T> IntoJoinValue<T> for T
where T: IntoExpr,

Source§

fn __value(self) -> Expr

Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T, N> NotifiedBy<T> for N
where T: Notification<N>,

Source§

fn __routes(&self) -> Vec<Route>

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, V> UpdateValue<T> for V
where V: FillValue<T>,

Source§

fn __update<M, U>(self, column: Column<M, T>, update: U) -> U
where M: Model, U: Updates<M>, T: IntoExpr + Clone + Send + Sync + 'static,

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more