Laravel where 子句在多个 orWhere 子句后不起作用?

Ami*_*man 2 php mysql laravel eloquent laravel-5

我正在查询带有型号名称和类型的车辆,但这些 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);
Run Code Online (Sandbox Code Playgroud)

Ija*_*een 7

分组你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(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);
Run Code Online (Sandbox Code Playgroud)

参考:https : //laravel.com/docs/5.7/queries#parameter-grouping