-
Notifications
You must be signed in to change notification settings - Fork 23
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
- Build an HTTP request to retrieve an assembly of models
- 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