我正在查询具有模型名和类型的车辆,但是这些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);
最佳答案
把你的,
$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(function($query) use($term){
$query->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);
参考:https://laravel.com/docs/5.7/queries#parameter-grouping