All models provided as part of the Aero core platform are Eloquent models. These can be extended with custom methods, which will typically interact with the model they are attached to in some way. This functionality is especially useful when creating custom Eloquent relationships.
For example, if a module provided reviews
for products, the reviews
method can be added to the Product model using a macro
:
\Aero\Catalog\Models\Product::macro('reviews', function () {
return $this->hasMany(\Acme\MyModule\Models\Review::class);
});
The relationship query builder can be accessed by referencing the method:
$approvedReviewCount = $product->reviews()->where('approved', true)->count();
Just like a typical Eloquent relationship on a model, the resulting Collection
of reviews can be accessed through the magic property:
$reviews = $product->reviews;
{% for review in product.reviews %}
...
{% endfor %}