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>
impl<M, T> Column<M, T>
Sourcepub fn eq(self, value: impl ComparableTo<T>) -> Condition
pub fn eq(self, value: impl ComparableTo<T>) -> Condition
Matches rows where the column equals value.
On a column of an Option type, eq(None) matches rows where the column is NULL.
Sourcepub fn ne(self, value: impl ComparableTo<T>) -> Condition
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.
Sourcepub fn lt(self, value: impl ComparableTo<T>) -> Condition
pub fn lt(self, value: impl ComparableTo<T>) -> Condition
Matches rows where the column is less than value.
Sourcepub fn le(self, value: impl ComparableTo<T>) -> Condition
pub fn le(self, value: impl ComparableTo<T>) -> Condition
Matches rows where the column is less than or equal to value.
Sourcepub fn gt(self, value: impl ComparableTo<T>) -> Condition
pub fn gt(self, value: impl ComparableTo<T>) -> Condition
Matches rows where the column is greater than value.
Sourcepub fn ge(self, value: impl ComparableTo<T>) -> Condition
pub fn ge(self, value: impl ComparableTo<T>) -> Condition
Matches rows where the column is greater than or equal to value.
Sourcepub fn is_in(self, values: impl ComparableList<T>) -> Condition
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.
Sourcepub fn is_not_in(self, values: impl ComparableList<T>) -> Condition
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.
Sourcepub fn between(
self,
low: impl ComparableTo<T>,
high: impl ComparableTo<T>,
) -> Condition
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.
Sourcepub fn expr(self) -> Expr
pub fn expr(self) -> Expr
Returns the column as an expression qualified with its table, such as "users"."email".
Sourcepub fn count_distinct(self) -> Expr
pub fn count_distinct(self) -> Expr
Counts distinct non-null values in this column (COUNT(DISTINCT column)).
Source§impl<M, T> Column<M, Option<T>>
impl<M, T> Column<M, Option<T>>
Sourcepub fn is_null(self) -> Condition
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.
Sourcepub fn is_not_null(self) -> Condition
pub fn is_not_null(self) -> Condition
Matches rows where the column isn’t NULL.
Source§impl<M, T> Column<M, T>where
ZonedDateTime: ComparableTo<T>,
Conditions that compare a Timestamp or Option<Timestamp> column with
laravel::now().
impl<M, T> Column<M, T>where
ZonedDateTime: ComparableTo<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();Sourcepub fn is_past(self) -> Condition
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.
Sourcepub fn is_future(self) -> Condition
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.
Sourcepub fn is_today(self) -> Condition
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.
Sourcepub fn is_before_today(self) -> Condition
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.
Sourcepub fn is_after_today(self) -> Condition
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,
impl<M, T> Column<M, T>where
T: __Text,
Sourcepub fn like(self, pattern: impl AsRef<str>) -> Condition
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.
Sourcepub fn like_case_sensitive(self, pattern: impl AsRef<str>) -> Condition
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.
Sourcepub fn starts_with(self, text: impl AsRef<str>) -> Condition
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.
Trait Implementations§
impl<N, T> ComparableTo<Option<T>> for Column<N, T>
impl<N, T> ComparableTo<T> for Column<N, T>
impl<N, T> ComparableTo<T> for Column<N, Option<T>>
impl<M, T> Copy for Column<M, T>
impl<M, T, V> InOrderOfColumn<V> for Column<M, T>where
V: ComparableTo<T>,
Source§impl<M, T> IntoColumn for Column<M, T>
impl<M, T> IntoColumn for Column<M, T>
Source§fn into_column(self) -> Expr
fn into_column(self) -> Expr
impl<M, C, T> OrderColumn<M> for Column<C, T>
Auto Trait Implementations§
impl<M, T> Forget for Column<M, T>
impl<M, T> Freeze for Column<M, T>
impl<M, T> Move for Column<M, T>
impl<M, T> RefUnwindSafe for Column<M, T>
impl<M, T> Send for Column<M, T>
impl<M, T> Sync for Column<M, T>
impl<M, T> Unpin for Column<M, T>
impl<M, T> UnsafeUnpin for Column<M, T>
impl<M, T> UnwindSafe for Column<M, T>where
&'static str: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<K> ForeignKey<K> for K
impl<K> ForeignKey<K> for K
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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