查询 laravel returns 空响应

Query in laravel returns empty response

我正在尝试使用 laravel 中的 leftJoin 从我的数据库中检索数据。我的约会中有两个 table 名称 usersappointments table 有一个字段名称 doctor_id 保存与用户 ID 相关的医生 ID。两者的 id 值相同。如果 users table 将 appointments table 中的列约会值与 where 子句 where patient_id = $patientID 匹配。但是当我执行查询时,它 returns 空响应。

代码

$patientID = $request->patientID;
$prevAppointments = DB::table('appointments')
  ->leftJoin('users', 'appointments.doctor_id', '=', 'users.id')
  ->select(
    'users.first_name',
    'users.last_name',
    'appointments.appointment_date',
    'appointments.status'
  )
  ->where('appointments.patient_id', $patientID)
  ->get();
return($prevAppointments);

用户table

约会table

试试这个 -

$patientID = $request->patientID;
$prevAppointments = DB::table('appointments')
                        ->leftJoin('users', 'appointments.doctor_id', '=', 'users.id')
                        ->select('users.first_name','users.last_name','appointments.patient_id', 'appointments.appointment_date','appointments.status')
                        ->where('appointments.patient_id',$patientID)
                        ->get();
return($prevAppointments);

使用此命令创建模型(如果您没有任何约会模型): 在您的项目类型的根目录中:

php artisan make:model Appointment

在创建的文件中写入这些:

protected $fillable=['patient_id','doctor_id','appointment_date','status'];

public function doctor(){
    $this->belongsTo(User::class,'doctor_id');
}

现在您可以使用此 Eloquent 查询: 首先添加预约 class:

 Use App\Appointment;

然后在方法中使用它:

 $prevAppointments = Appointment->where('patient_id','=',$patientID)->first();

现在您有一个包含预约医生属性的集合。有关 Eloquent ORM 和 Laravel 集合的更多详细信息,请遵循此 link:https://hamidshariati.ir/most-important-articles-to-learn-laravel/