laravel - orWhere giving unexpected result -


i want search pages lang_id = 1, using following laravel query

$result =  $this->where('lang_id', $this->langid ); $result->where('title', 'like', '%'.$search.'%')        ->orwhere('description', 'like', '%'.$search.'%'); $result->get(); 

it gives me pages having lang_id other 1 too.

how fix it?

it because build query return records have lang_id , title, or description starting search keyword. , that's why results, because of them don't meet lang_id requirement, meet description match.

you need group conditions properly, this:

$result =  $this->where('lang_id', $this->langid ) ->where(function ($query) use ($search) {     $query->where('title', 'like', '%'.$search.'%')->orwhere('description', 'like', '%'.$search.'%') }); $result->get(); 

this equal to:

select * table lang_id = $lang_id , (     title '%$search%' or description '%$search%' ) 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -