简体   繁体   中英

Using laravel eloquent relation to retrieve all records except NULL

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

eg

I have relation setup

Plate model

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();

will return everything, even those who have NULL .

在此输入图像描述

All I want is plates which have projects attached to. I tried laravel documentation, but could't find anything. Is there also same approach for many relations

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

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

Docs on has : http://laravel.com/docs/5.1/eloquent-relationships#querying-relations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM