Skip to content

How to use Coloquent

DavidDuwaer edited this page Nov 27, 2017 · 17 revisions

The usage example below, of which the class definitions of Teacher, Student, School and Address are not shown, shows how Coloquent is used to

  1. Build an HTTP request to retrieve an assembly of models
  2. Retrieve full-fledged interrelated models from Coloquent's response
Teacher
    .where('gender', 'm')           // sets a filter
    .with('students')               // eager loads related models
    .with('schools.address')        // eager loads directly and indirectly related models
    .get()                          // submits the HTTP request, returns an ES6 Promise
    .then(function (response: PluralResponse) {
        let teachers = response.getData();
        foreach (let teacher of teachers) {
            let students = teacher.getStudents();
            let schools = teacher.getSchools();
            foreach (let school of schools) {
                let address = school.getAddress();
            }
        }
    });

Each aspect of this example and more is elaborated in the following subsections

Subsections

  1. Retrieving
  2. Creating and saving
  3. Deleting
  4. Relations

Clone this wiki locally