且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel 4雄辩/模特关系

更新时间:2023-11-17 18:30:28

我认为问题是你的 has()方法正在寻找每个特定产品上具有5种不同大小的产品,这将假设您将使用 $ this-> hasMany('Size') 在您的产品型号。在那里, :: where()方法返回结果,其中产品的大小为5。

I think that the problem is that your ::has() method is looking for products with exactly 5 different sizes on each specific product, which would assume that you would be using $this->hasMany('Size') in your Product model. Where as the ::where() method is returning results where the size of the product is 5.

他们使用文档的一个示例的注释。一个帖子将有一个注释列表。你可以找到至少有一个注释的帖子(即 Post :: has('comments') - > get())或者你可以找到有更多的帖子超过3条评论(即 Post :: has('comments','> =','3') - > get()

In the documentation they use an example of comments. A post will have a list of comments. You can find posts that have at least one comment (ie. Post::has('comments')->get()) or you can find posts that have more than 3 comments (ie. Post::has('comments', '>=', '3')->get()).

http://laravel.com/docs/eloquent#查询关系