douxi7219 2019-02-11 19:27
浏览 84
已采纳

Laravel / LaraCSV管理复杂的关系

I'm running into some issues with my Collection/Model relationships with regards to LaraCSV. Here is its documentation: https://github.com/usmanhalalit/laracsv#full-documentation. I have 3 models that interact right now: Doctor, Patient, Script

Doctor belongsToMany Patient
Patient belongsToMany Doctor

Patient hasMany Script
Script belongsTo Patient

I also created a relationship link inside of my Doctor model that can be used to tie Doctor to Script, but does not appear to work in this instance:

public function scripts() {
    $this->load(['patients.scripts' => function($query) use (&$relation) {
        $relation = $query;
    }]);

    return $relation;
}

What I am attempting to do is allow admin staff and our users to download CSV files that contain all of their scripts. While this works fine for admin staff as I can reference the models directly, I am not able to make it work for users because they are tied to the doctors, and I cannot seem to tie this into scripts as normal. Here is a perfectly working version for admin staff:

$doctors = Doctor::orderBy('last_name', 'asc')->get();
$patients = Patient::orderBy('last_name', 'asc')->get();
$scripts = Script::orderBy('prescribe_date', 'desc')->get();

$csvExporter->beforeEach(function ($script) use ($doctors, $patients) {
    $patient = $patients->where('id', $script->patient_id)->first();
    $doctor = $patient->doctors->first();
    $script->patient = $patient->full_name;
    $script->doctor = $doctor->full_name;
});

Here is how the user-specific version appears:

$doctors = User::
    find(Auth::user()->id)
    ->doctors()
    ->orderBy('last_name', 'asc')
    ->get();
$patients = Patient::orderBy('last_name', 'asc')->get();
$scripts = $doctors->scripts()->get();

Trying to chain in my Doctor model scripts() function results in an error: Method Illuminate\Database\Eloquent\Collection::scripts does not exist.

$doctors = User::
    find(Auth::user()->id)
    ->doctors()
    ->orderBy('last_name', 'asc')
    ->get();
$patients = array();
$scripts = array();
foreach ($doctors as $doctor_fe) {

    foreach ($doctor_fe->patients as $patient_fe) {
        $patients[] = $patient_fe;

        foreach ($patient_fe->scripts as $script_fe) {
            $scripts[] = $script_fe;
        }
    }
}

I also tried to pull the information using arrays, but unfortunately, it must be a Collection passed in via this error: Argument 1 passed to Laracsv\Export::addCsvRows() must be an instance of Illuminate\Database\Eloquent\Collection, array given

  • 写回答

2条回答 默认 最新

  • doutu1939 2019-02-13 03:28
    关注

    I settled by placing all of the patients belonging to the user's doctors through a foreach loop, then using another one to grab the patient's id. I then took the patient's id array and used the whereIn function to compare the Script's patient_id field to get the correct strips.

    $doctors = User::
        find(Auth::user()->id)
        ->doctors()
        ->orderBy('last_name', 'asc')
        ->get();
    
    $patients_array = array();
    
    foreach ($doctors as $doctor_fe) {
        $patients_fe = $doctor_fe->patients;
            foreach ($patients_fe as $patient_fe) {
                $patients_array[] = $patient_fe->id;
             }
         }
    
    $patients = Patient::orderBy('last_name', 'asc')->get();
    $scripts = Script::whereIn('patient_id', $patients_array)->get();
    
    $csvExporter->beforeEach(function ($script) use ($doctors, $patients) {
        $patient = $patients->where('id', $script->patient_id)->first();
        $patient_initials = substr($patient->first_name, 0, 1) . substr($patient->last_name, 0, 1);
        $doctor = $patient->doctors->first();
        $script->patient = $patient_initials;
        $script->doctor = $doctor->full_name;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 MIT控制器能控制不稳定系统吗
  • ¥15 公司代码X对业务伙伴X无效,处理方法?
  • ¥15 微信内链接跳转到浏览器打开怎么实现
  • ¥15 三角波可以直接加施密特电路整形到矩形波吗实物
  • ¥15 html,php,在使用html请求php文件时发生了错误,无法请求到php文件读取数据库并用javascript进行数据显示,刷新
  • ¥15 touchsocket udp组播
  • ¥20 MAC怎么安装Silverlight 插件?以及安装了怎么启用
  • ¥15 VS2012中查询语句无法填入解析,数值传不进去
  • ¥15 gis系统开发出现命名空间“ESRI.ArcGIS”中不存在类型或命名空间名“Analyst3D”报错
  • ¥15 怎么让ai定时给我发信息 c#或者python