0

this query is for codeigniter how to make it in laravel eloquent

 $this->db->select('*');
            $this->db->from('countries cun'); 
            $this->db->join('cities cit', 'cit.country_code=cun.country_code', 'left');
            $this->db->join('city_states cits', 'cits.country_code=cun.country_code', 'left');
            $this->db->join('timezone timz', 'timz.country_code=cun.country_code', 'left');
            $this->db->where('cun.country',$id);         
           return $query = $this->db->get()->result();
Ghulam Abbas
  • 515
  • 2
  • 10
  • 23

2 Answers2

1

Try this

return DB::table('countries AS cun')
    ->leftJoin('cities AS cit', 'cit.country_code', '=', 'cun.country_code')
    ->leftJoin('city_states AS cits', 'cits.country_code', '=', 'cun.country_code')
    ->leftJoin('timezone AS timz', 'timz.country_code', '=', 'cun.country_code')
    ->where('cun.country', $id)
    ->get(); 

Eloquent Model

return Country::leftJoin('cities AS cit', 'cit.country_code', '=', 'countries.country_code')
     ->leftJoin('city_states AS cits', 'cits.country_code', '=', 'countries.country_code')
     ->leftJoin('timezone AS timz', 'timz.country_code', '=', 'countries.country_code')
     ->where('countries.country', $id)
     ->get();  
Shalu Singhal
  • 553
  • 3
  • 8
0

See here

Countries::with(["cities", "city_states", "timezone"])->where("country", $id)->get();

Countries - Eloquent Model.

cities, city_states, timezone - relationships in the Countries model.

Community
  • 1
  • 1
Andrii Lutskevych
  • 1,349
  • 13
  • 23