如何查看数据是否已经存在laravel
How to check the data already exists or not laravel
我有 2 个 table。患者 table 和预约 table.
患者table:
- ID
- 姓名
- 病历编号 (norekmed)
Table 个预订
- ID
- idpatient
- idroom
如果我预约了,如何查询已经或未查询的患者资料?
检查是通过比较患者的预约表格字段和 norekmed table。
如果患者资料已经存在,我们可以进行预约。如果没有患者资料,我们将无法进行预约。
如果觉得这样不好,有更好的方法,我接受。
预订控制器(商店)
$this->validate($request,
[
'idpatient' => 'required|unique:reservation,idpatient',
'idroom' => 'required',
]);
Patient::where(function($query) {
$query->has('id')
->orHas('norekmed');
})->find(1);
$reservasi = new Reservasi();
$reservasi->idpatient = $request->idpatient;
$reservasi->idroom = $request->idroom;
$reservasi->save();
一个简单的解决方案可能是使用 firstOrCreate
and let the validator check if the patient id and room id exist。如果我这样做,我会做以下事情:
$this->validate($request, [
// This will check if the patient exists.
'idpatient' => 'required|exists:patients,id',
// This will check if the room exists.
'idroom' => 'required|exists:room,id',
]);
// Get the reservation or create one if it does not exist.
$reservasi = Reservasi::firstOrCreate([
'idpatient' => $request->idpatient,
'idroom' => $request->idroom,
]);
...
我有 2 个 table。患者 table 和预约 table.
患者table:
- ID
- 姓名
- 病历编号 (norekmed)
Table 个预订
- ID
- idpatient
- idroom
如果我预约了,如何查询已经或未查询的患者资料?
检查是通过比较患者的预约表格字段和 norekmed table。
如果患者资料已经存在,我们可以进行预约。如果没有患者资料,我们将无法进行预约。
如果觉得这样不好,有更好的方法,我接受。
预订控制器(商店)
$this->validate($request,
[
'idpatient' => 'required|unique:reservation,idpatient',
'idroom' => 'required',
]);
Patient::where(function($query) {
$query->has('id')
->orHas('norekmed');
})->find(1);
$reservasi = new Reservasi();
$reservasi->idpatient = $request->idpatient;
$reservasi->idroom = $request->idroom;
$reservasi->save();
一个简单的解决方案可能是使用 firstOrCreate
and let the validator check if the patient id and room id exist。如果我这样做,我会做以下事情:
$this->validate($request, [
// This will check if the patient exists.
'idpatient' => 'required|exists:patients,id',
// This will check if the room exists.
'idroom' => 'required|exists:room,id',
]);
// Get the reservation or create one if it does not exist.
$reservasi = Reservasi::firstOrCreate([
'idpatient' => $request->idpatient,
'idroom' => $request->idroom,
]);
...