laravel eloquent 关系有问题

Problem with laravel eloquent relationship

我是 laravel 的新手,在 Eloquent 关系方面遇到问题。我正在开发一个网站,其中 Users/doctors 有很多患者记录。所以我想与用户和患者模型建立一对多的关系。问题是我正在获取属于每个 users/doctors 的所有患者数据,显然我只需要属于经过身份验证的 user/doctor 的患者数据。我在这里做错了什么?

这是我的用户模型文件。 User.php。

class User extends Authenticatable{
use Notifiable,SoftDeletes;

protected $fillable = [
    'name', 'email', 'password',
];

protected $hidden = [
    'password', 'remember_token',
];

public function patient()
    {
        return $this->hasMany('Bioctor\patient','user_id');

        //return $this->belongsTo(patient::class);
    }
}

这是患者模型。 patient.php.

class patient extends Model{
use SoftDeletes;

   protected $fillable = [
     'user_id','patient_name', 'slug', 'age', 'sex', 'case_title','case_des','blood_group','report_image_url'
   ];

   public function user()
    {
      //  return $this->belongsTo();

       // return $this->hasMany(User::class);
    }
}

这是我的病人控制器代码。

class PatientController extends Controller{

public function __construct()
{
    $this->middleware('auth');
}

public function index()
{
    return response()->json(User::with(['patient'])->get());      
}

public function create()
{
    return view('patient.patientInput');
}

public function store(CreatePatientRequest $request)
{        

    $request->uploadPatientImage()
            ->storePatientImage();    
    return redirect()->back();
}

public function show($id)
{
    //
    //dd(User::with(['patient'])->get());
}

public function edit($id)
{
    //
}

public function update(Request $request, $id)
{
    //
}

public function destroy($id)
{
    //
}
}

这就是我在 edit.index Ior patientcontroller@index 路线上得到的。这是我浏览器中的 json 输出。我在数据库中有两个用户和四个患者条目。我正在获取每个用户的所有患者记录。

[{"id":1,"name":"rnlio1995","email":"rnlio1995@gmail.com","email_verified_at":null,"deleted_at":null,"created_at":"2018-10-10 15:36:58","updated_at":"2018-10-10 15:36:58","patient":[{"id":1,"user_id":"1","patient_name":"aaaaaaaaaaaaa","slug":"aaaaaaaaaaaaa","age":"44","sex":"Male","case_title":"cough","case_des":"fsfsdfdsf","blood_group":"A+","report_image_url":"patient\/aaaaaaaaaaaaa.jpg","deleted_at":null,"created_at":"2018-10-10 15:37:38","updated_at":"2018-10-10 15:37:38"},{"id":2,"user_id":"1","patient_name":"noor","slug":"noor","age":"7","sex":"Male","case_title":"cough","case_des":"hgfhfhf","blood_group":"A+","report_image_url":"patient\/rnlio1995_5bbf878505fa8.jpg","deleted_at":null,"created_at":"2018-10-11 17:25:25","updated_at":"2018-10-11 17:25:25"},{"id":3,"user_id":"1","patient_name":"noor","slug":"noor","age":"44","sex":"Male","case_title":"cough","case_des":"yuyguyguy","blood_group":"A+","report_image_url":"patient\/rnlio1995_5bbf8c18e75cc.jpg","deleted_at":null,"created_at":"2018-10-11 17:44:56","updated_at":"2018-10-11 17:44:56"}]},{"id":2,"name":"noor","email":"rn19@gmail.com","email_verified_at":null,"deleted_at":null,"created_at":"2018-10-11 17:47:01","updated_at":"2018-10-11 17:47:01","patient":[{"id":4,"user_id":"2","patient_name":"noor","slug":"noor","age":"55","sex":"Male","case_title":"cough","case_des":"hihiuh","blood_group":"A+","report_image_url":"patient\/noor_5bbf8cae477eb.jpg","deleted_at":null,"created_at":"2018-10-11 17:47:26","updated_at":"2018-10-11 17:47:26"}]}]

这是输出的图像。 JSON output

如果您只想要患者数据,那么您不想使用 with->with() 表示您想要获取您的数据以及您指定的关系。这就是为什么 User::with(['patient'])->get() 会为您提供所有用户以及所有患者数据。

此外,由于一位医生可能有多个患者,因此您应该使用 patients()(复数)定义关系以使事情更清楚:

public function patients()
{
    return $this->hasMany('Bioctor\Patient', 'user_id');
}

然后您可以使用以下代码获取指定用户的患者:

//find specified user
$user = User::findOrFail($id);

//fetch relations
$patients = $user->patients;

foreach ($patients as $patient) {
    //do what you intend to do with each patient
}

注意我们不使用带括号的 patients(),我们只是像普通属性一样访问关系。当然,您必须指定 $id 或使用 LaravelAuth::user() 门面来获取 specified/authenticated 用户。

更多信息,请查看官方手册: Laravel Eloquent Relationships