我正在查询具有型号名称和类型的车辆,但是这些where子句在添加orWhere
之后并没有结尾。当我评论或删除这些orWhere
子句时,它将起作用。
$models = Vehicle::join('vmodels', 'vmodels.vehicle_id', '=', 'vehicles.id')
->join('vehicletypes', 'vehicletypes.id', '=', 'vehicles.vehicletype_id')
->join('brands', 'brands.id', '=', 'vehicles.make')
->join('companies', 'brands.id', '=', 'companies.name')
->select('vehicles.slug as vslug', 'brands.name as brandname', 'vehicles.id as vid', 'vmodels.*', 'vehicletypes.name as vtype', 'companies.status as cstatus')
->where('brands.name', 'LIKE', "%{$term}%")
->orWhere('vmodels.name', 'LIKE', "%{$term}%")
->orWhere('vmodels.year', 'LIKE', "%{$term}%")
->orWhere('vehicletypes.name', 'LIKE', "%{$term}%")
->orWhere('vehicles.model', 'LIKE', "%{$term}%")
->where('companies.status', '=', 1 )
->where('vmodels.status', '=', 1)
->where('vehicles.status', '=', 1)
->paginate(30);
答案 0 :(得分:3)
将您的dict[propertyType](propertyType)
的样式分组,
orWhere
答案 1 :(得分:1)
如果要使用Laravel查询构建器为SQL查询模拟AND / OR语句,则应按以下步骤进行操作:
((A or B) and (C or D)) or (E)
ModelName::join(...)->join(...)
->where(function($query){
$query->where( A )
->orWhere( B )
})
->where(function($query){
$query->where( C )
->orWhere( D )
})->orWhere( E )
答案 2 :(得分:0)
How about this one
$models = Vehicle::join('vmodels', 'vmodels.vehicle_id', '=', 'vehicles.id')
->join('vehicletypes', 'vehicletypes.id', '=', 'vehicles.vehicletype_id')
->join('brands', 'brands.id', '=', 'vehicles.make')
->join('companies', 'brands.id', '=', 'companies.name')
->select('vehicles.slug as vslug', 'brands.name as brandname', 'vehicles.id as vid', 'vmodels.*', 'vehicletypes.name as vtype', 'companies.status as cstatus')
->where('brands.name', 'LIKE', "%{$term}%")
->where('companies.status', '=', 1 )
->where('vmodels.status', '=', 1)
->where('vehicles.status', '=', 1)
->orWhere(function($query) use ($term){
$query->where('vmodels.name', 'LIKE', "%{$term}%")
->where('vmodels.year', 'LIKE', "%{$term}%")
->where('vehicletypes.name', 'LIKE', "%{$term}%")
->where('vehicles.model', 'LIKE', "%{$term}%")
})
->paginate(30);