tp5 $yjdata=Db::name('patient')->where(['MobileNum'=>$request->param('MobileNum'),'cpid'=>$user['cpid']])->find(); if($yjdata){ $patientreport=$yjdata->patient_report; Db::name('patient')->where(['PatientNum'=>$yjdata['PatientNum']])->update($insertData); $patientreport->save($insertDatareport); }解决报错Trying to get property 'patient_report' of non-object
时间: 2024-03-05 10:53:55 浏览: 26
报错信息 "Trying to get property 'patient_report' of non-object" 提示你访问了一个非对象的属性,可能是在一个空对象上进行了属性访问。在你的代码中,$yjdata 是一个数组,而不是一个对象,所以无法通过 $yjdata->patient_report 来访问 patient_report 属性。
解决方法是将 $yjdata['patient_report'] 赋值给 $patientreport 变量,可以使用以下代码进行修改:
```
$yjdata = Db::name('patient')->where(['MobileNum'=>$request->param('MobileNum'), 'cpid'=>$user['cpid']])->find();
if ($yjdata) {
$patientreport = $yjdata['patient_report'];
Db::name('patient')->where(['PatientNum'=>$yjdata['PatientNum']])->update($insertData);
$patientreport->save($insertDatareport);
}
```
这样就可以避免报错了。
相关问题
tp5 patients主表和patientReport副表一对一关联 主键是pid 副键是pdid patientReport中有一个rtype字段 实现用这个格式 $query = Patients::with('PatientReport')->where($conditions); // $query = Patients::where($conditions); if ($request->has('PatientNum')) { $query = $query->where('PatientNum', $request->param('PatientNum')); } if ($request->has('PatientName')) { $query = $query->where('PatientName', 'like', '%' . $request->param('PatientName') . '%'); } if ($request->has('MobileNum')) { $query = $query->where('MobileNum' ,$request->param('MobileNum')); } if ($request->has('rtype')) { $query = $query->where('rtype' ,$request->param('rtype')); } $data = $query->order('pid desc')->paginate($pagination); return corsJSON(0, $data);通过rype字段查找主表和副表中的所有相关数据
可以使用以下代码实现:
```
$query = Patients::with(['PatientReport' => function($query) use ($request){
if ($request->has('rtype')) {
$query->where('rtype', $request->param('rtype'));
}
}])->where($conditions);
if ($request->has('PatientNum')) {
$query = $query->where('PatientNum', $request->param('PatientNum'));
}
if ($request->has('PatientName')) {
$query = $query->where('PatientName', 'like', '%' . $request->param('PatientName') . '%');
}
if ($request->has('MobileNum')) {
$query = $query->where('MobileNum', $request->param('MobileNum'));
}
$data = $query->order('pid desc')->paginate($pagination);
return corsJSON(0, $data);
```
这段代码使用了 Laravel 的 Eloquent ORM,通过 `with` 方法可以实现主表和副表的关联,并通过匿名函数对副表进行筛选条件的添加。注意,要在匿名函数中使用 `use` 关键字将 `$request` 变量传入,以便在闭包内使用。
值得注意的是,如果 `rtype` 字段存在于主表中,则不需要使用 `with` 方法,只需将 `rtype` 筛选条件添加到主表查询中即可。
public function search_page(Request $request){ $key = $request->input('query'); $news = []; $total = 0; $query = ''; if ($key){ $news = News::query()->where('news_title','like',"%$key%")->orderBy('created_at','desc')->paginate(15,['*'],'search-page')->withQueryString(); $total = $news->total(); $query = $key; } return view('home.search.list',compact('news','total','query')); }写注释
/**
* 搜索页面
*
* @param Request $request 请求对象
* @return \Illuminate\View\View 返回搜索结果页面视图
*/
public function search_page(Request $request){
$key = $request->input('query');
$news = [];
$total = 0;
$query = '';
// 如果关键词存在
if ($key){
// 根据新闻标题模糊查询新闻
$news = News::query()->where('news_title','like',"%$key%")->orderBy('created_at','desc')->paginate(15,['*'],'search-page')->withQueryString();
// 获取查询到的新闻总数
$total = $news->total();
// 保存查询关键词
$query = $key;
}
// 返回搜索结果页面视图,并传递相关数据
return view('home.search.list',compact('news','total','query'));
}