使用laravel eloquent关系来检索除NULL之外的所有记录

use*_*381 5 php laravel

有没有办法使用eloquent模型检索所有非空的记录.

例如

我有关系设置

板模型

public function project()
{
    return $this->hasOne('App\Models\Project');
}
Run Code Online (Sandbox Code Playgroud)

项目模型

public function plate()
{
    return $this->belongsTo('App\Models\Plate');
}
Run Code Online (Sandbox Code Playgroud)

如何检索所有有价值的记录.

尝试这个 return $p = \App\Models\Plate::with('project')->get();

将返回一切,即使是那些拥有的人NULL.

在此输入图像描述

我想要的只是plates附有项目的.我试过laravel文档,但找不到任何东西.many关系 也有同样的方法

Tho*_*Kim 23

您可以使用该has方法仅检索具有项目的印版.

\App\Models\Plate::with('project')->has('project')->get();
Run Code Online (Sandbox Code Playgroud)

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