Skip to content

How to use Coloquent

David Duwaer edited this page Feb 3, 2022 · 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(response => {
        const teachers = response.getData();
        foreach (const teacher of teachers) {
            const students = teacher.getStudents();
            const schools = teacher.getSchools();
            foreach (const school of schools) {
                const 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