relation()` and `$model->relation` in Laravel? " />
Understanding $model->relation() vs $model->relation
In Laravel, the syntax $model->relation() and $model->relation are often used to access model relationships. However, they differ significantly in their functionality.
$model->relation()
$model->relation() invokes the relationship method defined in the model. This method returns the actual relationship object, which allows further query customization. For example:
<code class="php">$distributors = $store->distributors()->where('priority', '>', 4);</code>
$model->relation
$model->relation retrieves the result of the relationship. Laravel dynamically creates getter methods for relationships, allowing you to access them as model properties. This syntax automatically fetches the relationship data and returns the result as a collection. Example:
<code class="php">$distributors = $store->distributors;</code>
Key Differences
Use Cases
The above is the detailed content of What\'s the Difference Between `$model->relation()` and `$model->relation` in Laravel?. For more information, please follow other related articles on the PHP Chinese website!