gpt4 book ai didi

laravel - Laravel 中访问模型关系的方式之间的差异

转载 作者:行者123 更新时间:2023-12-02 18:50:04 24 4
gpt4 key购买 nike

访问模型关系时,以下有什么区别:

$post->comments

或者

$post->comments()

最佳答案

https://laravel.com/docs/5.1/eloquent-relationships#one-to-one

Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic properties. Dynamic properties allow you to access relationship functions as if they were properties defined on the model

这个:

$posts = $user->posts;

实际上与此相同:

$posts = $user->posts()->get();

像这样使用它将导致获取关系实例:

$posts = $user->posts();

如果您想使用特定过滤器对其进行过滤,这会很方便,因为您现在可以对其应用查询生成器。

关于laravel - Laravel 中访问模型关系的方式之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37812987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com