Is there a way to retrieve all the records which has not null using eloquent model.

e.g.

我有关系

板块模型

public function project()
{
    return $this->hasOne('App\Models\Project');
}

project model

public function plate()
{
    return $this->belongsTo('App\Models\Plate');
}

How can I retrieve all the records which have value.

trying this return $p = \App\Models\Plate::with('project')->get();

会退还所有东西,即使是那些有NULL英镑的人.

enter image description here

我想要的是plates个有附加项目的项目.我try 了laravel文档,但什么都找不到.many关系也有同样的方法吗

推荐答案

You can use the has method to only retrieve plates that have a project.

\App\Models\Plate::with('project')->has('project')->get();

has:http://laravel.com/docs/5.1/eloquent-relationships#querying-relations上的文档

Laravel相关问答推荐

运行NPM Prod时出现VUE问题

我应该如何使用遵循依赖反转原则的工厂方法设计模式

为什么我的 Laravel 开发服务器不提供 public 文件夹下的文件?

按回车键时如何防止此功能运行?

如何获取多态多对多关系子查询的自动 id 列名?

无法在 Laravel 项目中安装 @vitejs/plugin-vue

Laravel where 子句只返回一个数据库条目

Guzzle - Laravel如何使用 x-www-form-url-encoded 发出请求

laravel 基本路径

会话中的 Laravel 存储数组

Laravel:如何在没有数据库的情况下对用户进行身份验证

在 laravel 5.2 中禁用特定路由的 Web 中间件

升级到 Laravel 5.2 会使所有会话失效

用于集合的 Laravel 5.5 API 资源

Laravel 5 Mime 验证

Laravel 无法创建根目录

laravel 队列 - 同步驱动程序如何工作?它是在单独的进程/线程还是主执行线程中执行?

在 vue.js 中使用 Laravel 的Gate/Authorization

Laravel 从公共删除目录

laravel - 无法在控制器构造函数中获取会话