四个表加入laravel雄辩

时间:2016-05-19 06:41:24

标签: laravel

此查询是针对codeigniter如何在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();

2 个答案:

答案 0 :(得分:1)

试试这个

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(); 

雄辩模型

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();  

答案 1 :(得分:0)

See here

Countries

cities - 雄辩的模特。

city_states模型中的

timezoneCountriespublic class MyClass{ public void print(){ system.out.println("Hi") } } - relationships