我正在做一些联接,并试图获取数据。我的查询生成器是:
$datasource = DB::table('vehicles')->join('brands', 'vehicles.brand_id', '=', 'brands.id')->join('sections', 'vehicles.section_id', '=', 'sections.id')->select('vehicles.*, vehicles.id AS vid');
但我收到此错误:
SQLSTATE [42S22]:未发现柱:1054未知列’vehicles.model,’在’字段列表’(SQL:选择vehicles。model,如 AS从vehicles内连接brands上vehicles。brand_id= brands。id内连接sections上vehicles。section_id= sections。id极限4偏移0)620线
vehicles
model,
AS
brands
brand_id
id
sections
section_id
我做错了什么?
您应该使用selectRaw()而不是select():
selectRaw()
select()
->selectRaw('vehicles.*, vehicles.id AS vid');
了解有关原始表达式的更多信息:http : //laravel.com/docs/5.0/queries#raw- expressions