lundi 15 mars 2021

Self Joins in Laravel 8 Framework

I'm working on this project were I'm using Laravel 8 as my backend frame work and I have to make a self Join, I have tried out this code but it did not work -the first join is the one I tried but failed- :

   public function get_all_medical_centers(){
    $data= DB:: table('medicalcenters')
    ->join('medicalcenters','medicalcenters.id',"=",'medicalcenters.parent_id')
    ->join('countries','medicalcenters.country_id',"=",'countries.id')
    ->join('states','medicalcenters.country_id',"=",'states.id')
    ->join('cities','medicalcenters.country_id',"=",'cities.id')
    ->select('medicalcenters.*', 'countries.name as country_name', 'states.name as state_name', 'cities.name as cities_name')
    ->get();
    return $data;
}

The thing is, in the database the medical centers table has some attributes that are the parent of other attributes and I want to get the name of the parent attribute when previewing the children to also preview the name of its parent and not just the id.

the table diagram: enter image description here




Aucun commentaire:

Enregistrer un commentaire