-
Notifications
You must be signed in to change notification settings - Fork 0
Defining relationships
Flexible can automatically include related models as nested objects during indexing. It achieves this functionality by introspecting the models and finding relations. Every model has full control over its serialisation by overriding the transform function (see below). Sometimes you do not want related models to be included from a base model. Flexible allows you to specify conditions in the comments of the relation function. It knows the following options:
- @follow NEVER
- @follow UNLESS {base model} (write as many UNLESS lines as base models to exclude)
- @follow FROM {base model} (write as many FROM lines as base models to include)
Use the first annotation to never follow the relation. Use the second annotation to specify that the relation should not be included when you are coming from a specific base model. For example, if you do not want the toys to be included on the Husband model you would specify is as follows:
class Child extends Eloquent {
...
/**
* @follow UNLESS Husband
* @return \Illuminate\Database\Eloquent\Relations
*/
public function toys()
{
return $this->belongsToMany('Toy');
}
}
Use the third annotation to specify that the relation should only be included when you are coming from a specific base model. For example, if you want the toys to be included only from the Wife model you would specify is as follows:
class Child extends Eloquent {
...
/**
* @follow FROM Wife
* @return \Illuminate\Database\Eloquent\Relations
*/
public function toys()
{
return $this->belongsToMany('Toy');
}
}
Make sure that you add the proper annotations to the relation functions so that Flexible knows when a function defines an Eloquent relation. See the example models for the proper annotations.