Model class abstract
The base Eloquent Model.
This abstract class provides the foundation for all Magic models. It handles attribute management, type casting, dirty tracking, and serialization.
Creating a Model
class User extends Model with HasTimestamps, InteractsWithPersistence {
@override String get table => 'users';
@override String get resource => 'users';
@override
List<String> get fillable => ['name', 'email'];
@override
Map<String, String> get casts => {'born_at': 'datetime'};
// Typed accessors
String get name => getAttribute('name');
set name(String val) => setAttribute('name', val);
}
Attribute Casting
The model automatically casts attributes based on the casts map:
datetime→ Carbonjson→ Map<String, dynamic>bool→ boolint→ intdouble→ double
- Implementers
Constructors
- Model()
Properties
-
appends
→ List<
String> -
The accessors to append to the model's array form.
no setter
-
attributes
→ Map<
String, dynamic> -
Get all attributes as a Map.
no setter
-
casts
→ Map<
String, dynamic> -
The attributes that should be cast.
no setter
- exists ↔ bool
-
Indicates if the model exists in the database.
getter/setter pair
-
fillable
→ List<
String> -
The attributes that are mass assignable.
no setter
-
guarded
→ List<
String> -
The attributes that are guarded from mass assignment.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
-
The attributes that should be hidden for serialization.
no setter
- id ↔ dynamic
-
Get the model's primary key value.
getter/setter pair
- incrementing → bool
-
Indicates if the primary key is auto-incrementing.
no setter
- primaryKey → String
-
The primary key for the model.
no setter
-
relations
→ Map<
String, Model Function()> -
The model relationships for automatic casting.
no setter
- resource → String
-
The API resource name for remote operations.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- table → String
-
The table associated with the model.
no setter
- useLocal → bool
-
Whether to use local database persistence.
no setter
- useRemote → bool
-
Whether to use remote API persistence.
no setter
-
visible
→ List<
String> -
The attributes that should be visible for serialization.
no setter
- wasRecentlyCreated ↔ bool
-
Indicates if the model was recently created.
getter/setter pair
Methods
-
append(
List< String> attributes) → Model - Append accessors to the model.
-
fill(
Map< String, dynamic> attributes, {bool strict = false}) → void - Fill the model with an array of attributes.
-
get<
T> (String key, {T? defaultValue}) → T? - Get an attribute value with optional default and type casting.
-
getAttribute(
String key) → dynamic - Get an attribute from the model.
-
getDirty(
) → Map< String, dynamic> - Get the dirty attributes (those that have changed).
-
getRelation<
T extends Model> (String key) → T? - Get a single related model.
-
getRelations<
T extends Model> (String key) → List< T> - Get a list of related models.
-
has(
String key) → bool - Check if an attribute exists and is not null.
-
isDirty(
[String? attribute]) → bool - Create a model instance from a Map.
-
makeHidden(
List< String> attributes) → Model - Make attributes hidden.
-
makeVisible(
List< String> attributes) → Model - Make attributes visible.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
set(
String key, dynamic value) → void - Set an attribute value.
-
setAttribute(
String key, dynamic value) → void - Set an attribute on the model.
-
setRawAttributes(
Map< String, dynamic> attributes, {bool sync = false}) → void - Set the model's raw attributes.
-
syncOriginal(
) → void - Sync the original attributes with the current.
-
toArray(
) → Map< String, dynamic> - Alias for toMap - Laravel-style naming.
-
toJson(
) → String - Convert the model to a JSON string.
-
toMap(
) → Map< String, dynamic> - Convert the model to a Map.
-
toString(
) → String -
A string representation of this object.
override
-
updateTimestamps(
) → void - Update the model's timestamps.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited