我的常规查询是这样的:
SELECT a.nationality_id, a.nationality_name, b.province_name, c.city_name
FROM tb_general_nationality a
JOIN tb_search_province b ON a.nationality_id = b.country_id
JOIN tb_search_city c ON b.province_id = c.province_id
WHERE b.country_id = 50
我将查询更改为laravel查询,如下所示:
服务:
public function listCity($param=null)
{
$city = City::with('province','country')->where('tb_search_province.country_id',$param)->get();
echo json_encode($city);
}
型号:
城市模型:
namespace App\Models\City;
use Illuminate\Database\Eloquent\Model;
class City extends Model {
protected $connection = 'pgsql';
protected $table = 'tb_search_city';
public function province()
{
return $this->belongsTo('App\Models\Province\Province','province_id','province_id');
}
}
国家模型:
namespace App\Models\Country;
use Illuminate\Database\Eloquent\Model;
class Country extends Model {
protected $connection = 'pgsql';
protected $table = 'tb_general_nationality';
}
省份型号:
namespace App\Models\Province;
use Illuminate\Database\Eloquent\Model;
class Province extends Model {
protected $connection = 'pgsql';
protected $table = 'tb_search_province';
public function country()
{
return $this->belongsTo('App\Models\Country\Country','nationality_id','country_id');
}
}
但是,我的laravel查询不起作用。 存在错误。错误是这样的:
SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "tb_search_province"
LINE 1: select * from "tb_search_city" where "tb_search_province"."c...
^ (SQL: select * from "tb_search_city" where "tb_search_province"."country_id" = 50)
我的表是这样的:
tb_search_city (city_id, province_id, city_name)
tb_search_province (province_id, country_id, province_name)
tb_general_nationality (nationality_id, nationality_name)
如何将常规查询更改为laravel查询?
非常感谢