我是laravel的新手,正面临着雄辩的关系问题。我正在开发一个网站,用户/医生拥有许多患者记录。因此,我想与用户和患者模型建立一对多的关系。问题是我要获得所有患者数据属于每个用户/医生,而我显然只需要属于该经过身份验证的用户/医生的患者数据。我在这里做错了什么?
这是我的用户模型文件。 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或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"}]}]
这是输出的图像。 this solution
答案 0 :(得分:2)
如果您只需要患者数据,则您不想使用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
或使用Laravel
的{{1}}门面来获取指定/经过身份验证的用户。
有关更多信息,请参阅以下官方手册: Laravel Eloquent Relationships