Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Tibebu's avatar

How to join two tables with conditions

I have three tables: 1. patients 2. finances tables, 3. vital what i want is to send patients to vital page after they paid in finance table if they have payment request to pay, otherwise, i want send them to vital if their status is active and payment value ==0

my finance model:

class Finance extends Model { use HasFactory; protected $table='finances'; protected $fillable = ['service_cost','service_name','recevied_by','pmrn'];

public function patien():HasOne
{
    return $this->hasOne(Patient::class,'id', 'mrn');
}

}

my patient model: class Patient extends Model { use HasFactory; protected $table='patients'; // public $timestamps = false; protected $fillable = ['mrn','f_name','m_name','l_name','sex','age','region','zone','woreda','kebele','village','house_no','phone','marital_status','email','status','cardpayamount','lastpaiddate'];

}

vital model:

class Vital extends Model { use HasFactory; protected $table='vitals'; // public $timestamps = false; protected $fillable = ['chief_complain','history_allergy','blood_pressure','pressure_rate','spo2','rr', 'pid','temperature','past_medical_illness','illnes_time','transfer_to','triaged_by'];

}

i want something like this in my vital index function page:

        $lastpaid = Finance::get();
        $pat = Patient::get();

        if ($lastpaid->pmrn == $pat->id && $lastpaid->updated_at == $pat->updated){
            $patients = DB::table('patients')->where('status', '1')->paginate(10); 
        }
        else{
            $patients = DB::table('patients')->where('status', '1')->where('cardpayamount', '0.0000', Carbon::today())->latest()->paginate(10);

        }
    
        return view('vital.index', compact('patients'));

but i have error message

Exception Property [pmrn] does not exist on this collection instance.

0 likes
8 replies
Sinnbeck's avatar

A few things. No need to show relationships if you dont plan to use eloquent. DB::table() cannot use those relationships.

Secondly, ::get() gets all patients/finances instead of just 1. Use ::find() or ::where('x, 1)->first()

Finally, if you ever need to set id as the second parameter in a relationship, you most likely have it reversed. A "finance" belongs to a patient

public function patient():BelongsTo
{
    return $this->belongsTo(Patient::class, 'mrn');
}

Also I am confused by why the column is named mrn and not patient_id

Tibebu's avatar

@Sinnbeck Thank You! What i want is to send paid patients from reception to vital via finance/cahser and other patients whom will not pay, i want to send them to directly vital room. MRN is medical record number

Therefore, what i think is one patient will have one record in finance table like this return $this->belongsTo(Patient::class, 'mrn');

i have three columns to check this :

  1. status in patient table, which 0/1 - inactive/active
  2. payment in patient table, which has value, which 0$/10$
  3. paymentstatus in finance table, with value 0/1 - paid/notpaid

in vital controller index function

    $lastpaid = Finance::get();
    $pat = Patient::get();

// i want to check if pmrn in finance table(foreignkey of patient table) == id patient primary key and values of updated_at in both tables, if that satisfies i want to display all active patients in vital page, if not i want to display patients with status active and payment value = 0; if ($lastpaid->pmrn == $pat->id && $lastpaid->updated_at == $pat->updated){ $patients = DB::table('patients')->where('status', '1')->paginate(10); } else{ $patients = DB::table('patients')->where('status', '1')->where('cardpayamount', '0.0000', Carbon::today())->latest()->paginate(10);

    }

    return view('vital.index', compact('patients'));
Sinnbeck's avatar

@Tibebu But you are saying you want to redirect the patient when they are logged in, yet you are showing code for pagination of all patients? I'm a bit confused

Tibebu's avatar

@Sinnbeck Thank you again:

// i want to check if pmrn in finance table(foreignkey of patient table) == id patient primary key and values of updated_at in both tables, if that satisfies i want to display all active patients in vital page, if not i want to display patients with status active and payment value = 0;. I want those patient after they pay in finance table, back to vital room

    <div class="card-body">
      <table id="patientTable" class="table">
        <thead>
          <tr>
            <th>Sl</th>
            <th>MRN</th>
            <th>First Name</th>
            <th>Middle Name</th>
            <th>Last Name</th>
            {{-- <th>Status</th> --}}
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
            
            @foreach($patients as $patient)
          {{-- <tr id="pid{{$patient->id}}"> --}}
            <td>{{ $patient->id }}</td>
            <td>{{ $patient->mrn }}</td>
            <td>{{ $patient->f_name }}</td>
            <td>{{ $patient->m_name }}</td>
            <td>{{ $patient->l_name }}</td>
            <td>
            <a href="{{url('vitals/'.$patient->id.'/edit')}}"  class="btn btn-info editbtn" >
              <i class="fa fa-edit"></i>Manage Patient</a>
             </td> 
            </tr>
              @endforeach
            </tbody>
          </table>
          {{ $patients->links() }}
    </div>
  </div>
</div>
Tibebu's avatar

Hello guys! I have three employees with rotating shift. What i want is, i want to get all the data changes on my sales table during login user up to the new employer takes the office.

lalitesh's avatar

@Tibebu I suggest you provide a little more details.

These 3 employees, which I believe, is a user of a particular user group (employees), so when any of them is logged in and they perform any IT operation in the system, say registration of a patient, that employee ID should be saved as "created_by_id" in the patients table.

Tibebu's avatar

@lalitesh Thank You! yes i have created_by field for them, but what i want is to fetch data of that table based on login user until the next shift employer takes that task. Therefore i can audit them.

Please or to participate in this conversation.