jeudi 15 juillet 2021

Which one Realtionship Suitable In this Case. - Laravel

I have 3 Table Like This,

One of User Table

users
  id,
  name

second one

documents
  id,
  type

third One

  clinician_credentials
     id,
     user_id,
     document_id => in this user_id from users table and document_id from documents table

I am tried hasManyThrough but did not give perfect data, please suggest to me which one relationship is suitable in this case.

public function clinician_credential() {
   return $this->hasManyThrough(User::class, ClinicianCredentials::class, 'id', 'user_id', 'id', 'clinician_id');
}

Then I am using for each loop to get clinician_credential but I want to use eager loading, not for each loop.

Right Now My query Like this,

    $documents = Documents::orderBy('type')->get()->toArray();
    if (count($documents)) {
        foreach ($documents as &$document) {
            $document['dp_credential'] = null;
            $clinician_credential = ClinicianCredentials::where('document_type', $document['id'])
                    ->where('clinician_id', $user_id)
                    ->select(DB::raw('*, datediff(expiry_date, now()) as days, IF(datediff(expiry_date, now()) < 30, 1, 0) as is_expiry'))
                    ->first();
            if ($clinician_credential) {
                $document['dp_credential'] = $clinician_credential;
            }
 


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire