Function
Static Public Summary | ||
public |
Define or retrieve a model definition. |
Static Public
public Eloquent(name: string, definition: Object | function(base: Model): Model | undefined): * source
import Eloquent from 'eloquentjs/src/index.js'
Define or retrieve a model definition.
Params:
Name | Type | Attribute | Description |
name | string | ||
definition | Object | function(base: Model): Model | undefined |
|
The definition can be either an object of properties to merge into the class, or a callback that receives the base class and returns an extended class definition. Or, omit this argument to fetch the named model. |
Return:
* |
Example:
// Define an Eloquent model with an object that
// extends the base Model definition.
Eloquent('Post', {
endpoint: 'api/posts'
});
// Define an Eloquent model with a callback
Eloquent('Post', function (modelDefinition) {
modelDefinition.endpoint = 'api/posts';
return modelDefinition;
});
// Fetch a previously defined model
let Post = Eloquent('Post');
// or
let Post = Eloquent.Post;
// It's (mostly) the same API as Laravel's Eloquent
// so you already know how to query the posts table...
Post.whereNotNull('published')
.orderBy('published')
.get()
.then(function (results) {
console.log(results);
});
// ... or save a new record
Post.create({
author: 'Derek',
body: 'Hello!'
});