如何放弃选择与另一个 table 的空关系项

How to discard selecting empty relationship items with another table

我正在使用 laravel eloquent.

查询如下数据库
PostModel::with('images')->where('id', $id)->where('is_active', 1)->paginate(10);

在某些情况下,某些帖子没有图片,因此 images 属性 的数组为空。我需要知道的是如何丢弃具有 empty array for images 的结果。我不想 运行 a foreach 并删除项目,我正在查询端寻找解决方案,比如放弃对这些项目的选择。谢谢。

了解 has()whereHas() Eloquent、url 的方法:http://laravel.com/docs/5.1/eloquent-relationships#querying-relations(标题:查询关系是否存在

解决方案:

PostModel::with('images')->has('images')->where('id', $id)->where('is_active', 1)->paginate(10);

or 您可以使用 join()(性能更高):http://laravel.com/docs/5.1/queries#joins