Php 拉威尔雄辩关系的问题

Php 拉威尔雄辩关系的问题,php,laravel,eloquent,laravel-query-builder,Php,Laravel,Eloquent,Laravel Query Builder,我是新来的拉雷维尔,面临着与雄辩的关系的问题。我正在开发一个网站,其中用户/医生有许多患者记录。因此,我想与用户和患者模型建立一对多关系。问题是,我获取的所有患者数据都属于每个用户/医生,而我显然只需要属于经过身份验证的用户/医生的患者数据。我做错了什么 这是我的用户模型文件。User.php class User extends Authenticatable{ use Notifiable,SoftDeletes; protected $fillable = [ 'name', '

我是新来的拉雷维尔,面临着与雄辩的关系的问题。我正在开发一个网站,其中用户/医生有许多患者记录。因此,我想与用户和患者模型建立一对多关系。问题是,我获取的所有患者数据都属于每个用户/医生,而我显然只需要属于经过身份验证的用户/医生的患者数据。我做错了什么

这是我的用户模型文件。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"}]}]
这是输出的图像。

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

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

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
或使用
Laravel
Auth::user()
facade来获取指定的/经过身份验证的用户

有关更多信息,请参阅以下官方手册:

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

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

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
或使用
Laravel
Auth::user()
facade来获取指定的/经过身份验证的用户

有关更多信息,请参阅以下官方手册:

您正在使用的Laravel版本是什么?它是Laravel 5.7。您正在使用的Laravel版本是什么?它是Laravel 5.7。