根据模型中关系的数据过滤搜索结果

Filter search results based on data of a relationship in a model

在这种情况下,我有 患者模型 报告模型 。其中 Patients 有许多报告。

<?php

namespace App;  
use Illuminate\Foundation\Auth\User as Authenticatable;
use DB;
use Spatie\Permission\Traits\HasRoles;

class Patient extends Authenticatable
{
    public function reports()
    {
        return $this->hasMany('App\Reports');
    }
}

在一个视图中,我有一个所有患者的列表,其中包含针对他们的报告 ID。我们有一个搜索患者模块,我们可以在其中搜索具有 患者 ID 报告 ID 的患者。我能够使用

满足对 患者 ID 的搜索

$data = Patient::where("id", "LIKE", "%{$search_patient}%")

但无法解决根据 报告 ID 搜索患者和筛选结果的场景,因为正在使用 hasMany 关系

检索报告数据

下面是结果,其中 Patient 数据来自 Patient 模型,Reports 数据来自使用 hasMany 关系的 Reports 模型。我的要求是,当我使用报告 ID 进行搜索时,我应该只能看到具有该报告 ID 的数据和用户信息。

[
  {
    "id": 1,
    "group_id": 1000,
    "date": "01-01-14",
    "name": "Voss",
    "address": "My Home 1",
    "reports": [
      {
        "id": "ABC123",
        "name": "Report1"
      },
      {
        "id": "EDC123",
        "name": "Report2"
      }
    ]
  },
  {
    "id": 2,
    "group_id": 1000,
    "date": "01-01-15",
    "name": "Rosalia",
    "address": "My Home 2",
    "reports": [
      {
        "id": "RTC123",
        "name": "Report3"
      },
      {
        "id": "TYH123",
        "name": "Report4"
      }
    ]
  }
]

像这样向您的报表模型添加 belongsTo 关系:

public function patient() {
    return $this->belongsTo("App\Models\Patient", "patient_id", "id");
}

然后在报表模型中执行搜索

$data = Report::with('patient')->where('id', $report_id)->get();

结果应该是这样的

...
{
  "id": ABC123,
  "name": Report 1,
  "patient": {
    "id": 2,
    "group_id": 1000,
    "date": "01-01-15",
    "name": "Rosalia",
    "address": "My Home 2",
  }
},
...

编辑: 如果你坚持以你的病人为基础,那么这样做:

 Patient::whereHas('reports', function ($query) use($report_id) {
     $query->where('id', $report_id);
 })->with(['reports' => function ($query) use($report_id) {
     $query->where('id', $report_id);
 }])->get();
  1. whereHas 将执行搜索
  2. with 将仅预先加载您指定的报告 ID

首先在您的报表模型中添加一个 belongsTo:

public function patient()
{
    return $this->belongsTo(Patient::class);
}

然后像这样检索它:

$reportId = "ABC123"; //for demo purposses, you get this dynamically

$record = Report::where('record_id', '=', $reportId)->first();
    // i used record_id, but you probably have just id;

$patient = $record->patient()->with('records')->first();

dd($patient);

结果是这样的:

Patient {#199 ▼
  #guarded: []
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:5 [▼
    "id" => 1
    "group_id" => 1000
    "date": "01-01-15"
    "name" => "Rosalia"
    "created_at" => null
    "updated_at" => null
  ]
  #original: array:5 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [▼
    "reports" => Collection {#204 ▼
      #items: array:2 [▼
        0 => Report {#207 ▶}
        1 => Report {#209 ▶}
      ]
    }
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []