I have a question regarding the WHERE clause
in the query I would like to say
SELECT ...
FROM ...
WHERE link.hos_id = $hos_id
AND outcome.otc_otrdischargedate = $td
AND outcome.otc_outcome LIKE ['%ICU%', '%I.C.U%', '%L3%']
but instead it reads like this
SELECT ...
FROM ...
WHERE link.hos_id = $hos_id
AND outcome.otc_otrdischargedate = $td
OR outcome.otc_outcome LIKE ['%ICU%', '%I.C.U%', '%L3%']
how should the laravel query be structured to read like the first example?
$matchThese = ['link.hos_id' => $hos_id, 'outcome.otc_otrdischargedate' => $td];
$icu = DB::table('link')
->join('demographic', 'link.lnk_dmgid', '=', 'demographic.dmg_id')
->join('admission', 'link.lnk_admid', '=', 'admission.adm_id')
->join('outcome', 'link.lnk_otcid', '=', 'outcome.otc_id')
->where($matchThese)
->orWhere('outcome.otc_outcome', 'like', '%ICU%')
->orWhere('outcome.otc_outcome', 'like', '%I.C.U%')
->orWhere('outcome.otc_outcome', 'like', '%L3%')
->orWhere('outcome.otc_outcome', 'like', '%Level3%')
->orWhere('outcome.otc_outcome', 'like', '%Level 3%')
->orWhere('outcome.otc_outcome', 'like', '%Intensive Care Unit%')
->get();
$icuSize = sizeof($icu);
SOLUTION -- THANKS #Mehravish Temkar
different query, but same principal
$array_conditions = array('%ICU%', '%I.C.U%','%L3%','%Level3%','%Level 3%','%Intensive Care Unit%');
$step_down_icu = DB::table('link')
->join('daily_link', 'link.lnk_id', '=', 'daily_link.dlk_lnkid')
->join('admission', 'link.lnk_admid', '=', 'admission.adm_id')
->join('outcome', 'link.lnk_otcid', '=', 'outcome.otc_id')
->where('admission.adm_referraldate', '=', $td)
->where('outcome.otc_outcome', 'like', '%ICU%')
->Where(function ($query) use($array_conditions) {
for ($i = 0; $i < count($array_conditions); $i++){
$query->orwhere('outcome.otc_outcome', 'like', '%' . $array_conditions[$i] .'%');
}
})->get();
$step_down_icuSize = sizeof($step_down_icu);
This should work:
$array_conditions = array('%ICU%', '%I.C.U%','%L3%');
$matchThese = ['link.hos_id' => $hos_id, 'outcome.otc_otrdischargedate' => $td];
$icu = DB::table('link')
->join('demographic', 'link.lnk_dmgid', '=', 'demographic.dmg_id')
->join('admission', 'link.lnk_admid', '=', 'admission.adm_id')
->join('outcome', 'link.lnk_otcid', '=', 'outcome.otc_id')
->where($matchThese)
->whereIn('outcome.otc_outcome', 'like', $array_conditions)
->get();
$icuSize = sizeof($icu);
Just switch the orWhere() with where(). The query builder will chain the SQL clauses with AND.
$matchThese = ['link.hos_id' => $hos_id, 'outcome.otc_otrdischargedate' => $td];
$icu = DB::table('link')
->join('demographic', 'link.lnk_dmgid', '=', 'demographic.dmg_id')
->join('admission', 'link.lnk_admid', '=', 'admission.adm_id')
->join('outcome', 'link.lnk_otcid', '=', 'outcome.otc_id')
->where($matchThese)
->where('outcome.otc_outcome', 'like', '%ICU%')
->where('outcome.otc_outcome', 'like', '%I.C.U%')
->where('outcome.otc_outcome', 'like', '%L3%')
->where('outcome.otc_outcome', 'like', '%Level3%')
->where('outcome.otc_outcome', 'like', '%Level 3%')
->where('outcome.otc_outcome', 'like', '%Intensive Care Unit%')
->get();
$icuSize = sizeof($icu);
Related
I'm new in Laravel and Livewire. I have problem with my code, I'm trying to use where clauses and search in one query. I have code like this.
This is in my controller
public function read()
{
return Groom::query()
->search($this->search)
->orderBy($this->sortBy, $this->sortDirection)
->paginate($this->perPage);
}
and this one in my model
public function scopeSearch($query, $val)
{
return $query
->where('status', '<>', 'selesai')
->leftJoin('pets', 'pets.id', '=', 'grooms.pet_id')
->where('name', 'like', '%' .$val. '%')
->where(function ($query) use ($val) {
$query
->Orwhere('service', 'like', '%'. $val. '%')
->Orwhere('address', 'like', '%' .$val. '%')
->Orwhere('status', 'like', '%' .$val. '%');
});
}
There's no error there, but my search isn't working
Your generated SQL query is
select * from grooms
left join "pets" on "pets"."id" = "grooms"."pet_id"
where "status" <> 'selesai'
and "name" like ? and (
"service" like ?
or "address" like ?
or "status" like ?
)
order by ...
Perhaps you want the name filter to be inside that grouped where? Try
public function scopeSearch($query, $val)
{
return $query
->where('status', '<>', 'selesai')
->leftJoin('pets', 'pets.id', '=', 'grooms.pet_id')
->where(function ($query) use ($val) {
$query
->orWhere('name', 'like', '%' .$val. '%')
->orwhere('service', 'like', '%'. $val. '%')
->orwhere('address', 'like', '%' .$val. '%')
->orwhere('status', 'like', '%' .$val. '%');
});
}
I am trying to get users with customer's role only along with their profile details in laravel. my query is like this
$customer = User::with(['roles' => function ($query) {
$query->where('name', '=', 'customer');
}])->join('profiles', 'profiles.user_id', '=', 'users.id')
->join('cities', 'cities.id', '=', 'profiles.city')
->where([['users.email', 'LIKE', '%'.$email.'%'], ['profiles.mobile', 'LIKE', '%'.$mobile.'%'], ['profiles.name', 'LIKE', '%'.$name.'%'], ['cities.name', 'LIKE', '%'.$city.'%'], ['profiles.home_address', 'LIKE', '%'.$address.'%']])
->select('profiles.name','profiles.mobile','profiles.home_address','cities.name as city', 'users.id','users.email')
->orderBy($sort_field, $sort_type)
->paginate($pagination);
But i am getting all role's records.
Can anyone help or look at this.
Thanks in advance!!
Below Code is working
$customer = User::whereHas(
'roles', function($q){
$q->where('name', 'customer');
}
)->join('profiles', 'profiles.user_id', '=', 'users.id')
->join('cities', 'cities.id', '=', 'profiles.city')
->where([['users.email', 'LIKE', '%'.$email.'%'], ['profiles.mobile', 'LIKE', '%'.$mobile.'%'], ['profiles.name', 'LIKE', '%'.$name.'%'], ['cities.name', 'LIKE', '%'.$city.'%'], ['profiles.home_address', 'LIKE', '%'.$address.'%']])
->select('profiles.name','profiles.mobile','profiles.home_address','cities.name as city', 'users.id','users.email')
->orderBy($sort_field, $sort_type)
->paginate($pagination);
I need to search in multiple tables. I'm checking every single request object. For example: I am checking if the request has that object then concating it to my main query and getting that query result at the last. It doesn't looks and isn't good. How can I make this search query filter better in laravel?
Note: I have searched questions in stackoverflow but they are dealing with only one model.
$query = DB::table('clients')
->leftjoin('ecommerce_contacts','ecommerce_contacts.client_id', '=', 'clients.id')
->select('ecommerce_contacts.*', 'clients.*')
->where('clients.is_deleted', '=', '0');
if(!is_null($request->fname)){
$query+=->where('clients.fname', 'like', '%$request->fname%');
}
if(!is_null($request->lname)){
$query+=->where('clients.lname', 'like', '%$request->lname%');
}
if(!is_null($request->gender)){
$query+=->where('clients.sex', $request->sex);
}
if(!is_null($request->number)){
$query+=->where('ecommerce_contacts.sex', 'like', $request->number);
}
if(!is_null($request->registered_date)){
}
if(!is_null($request->purchase)){
}
$client = $query->get();
$data = json_encode($clients);
return $data;
Use conditional clauses:
DB::table('clients')
->leftjoin('ecommerce_contacts','ecommerce_contacts.client_id', '=', 'clients.id')
->select('ecommerce_contacts.*', 'clients.*')
->where('clients.is_deleted', '=', '0')
->when(request()->has('fname'), function ($query) {
return $query->where('clients.fname', 'like', '%' . request()->fname . '%');
})
->when(request()->has('lname'), function ($query) {
return $query->where('clients.lname', 'like', '%' . request()->lname . '%');
})
->when(request()->has('gender'), function ($query) {
return $query->where('clients.sex', '=', request()->gender);
})
...
$query=DB::table('clients')
->leftjoin('ecommerce_contacts','ecommerce_contacts.client_id', '=', 'clients.id')
->select('ecommerce_contacts.*', 'clients.*')
->where('clients.is_deleted', '=', '0');
$search_fields=['fname','lname','gender','number','registered_date','purchase'];
foreach($search_fields as $key){
if(!is_null($key)){
$query->orWhere('clients.'.$key, 'LIKE', '"%" . '.$request->$key.' . "%"');
}
}
$client = $query->get();
$data = json_encode($client);
return $data;
I upvoted #DigitalDrifter 's answer because i liked it but I prefer my filter pattern.
Have a look at this:
$query = DB::table('clients')
->leftjoin('ecommerce_contacts','ecommerce_contacts.client_id', '=', 'clients.id')
->select('ecommerce_contacts.*', 'clients.*')
->where('clients.is_deleted', '=', '0');
!isset($request->fname) ?: $query->where('clients.fname', 'like', '%$request->fname%');
!isset($request->lname) ?: $query->where('clients.lname', 'like', '%$request->lname%');
!isset($request->gender) ?: $query->where('clients.sex', $request->sex);
!isset($request->number) ?: $query->where('ecommerce_contacts.sex', 'like', $request->number);
$client = $query->get();
$data = json_encode($clients);
return $data;
I think this is more readable and requires less line of code.
hello everyone i try to run a search query with a condition for my column 'structure_id' but when i run the query , it display me also results who are not from 'structure' = 4
here my query :
$search = $request->get('q');
return User::where(function($q) use($search) {
$q->where('name', 'like', '%' . $search .'%')
->orWhere('email', 'like', '%'.$search.'%')
->where('structure_id' , '=' , '4');
})->get();
someone have an idea to resolve this? thanks a lot in advance
change it to
$search = $request->get('q');
return User::where(function($q) use($search) {
$q->where('name', 'like', '%' . $search .'%')
->orWhere('email', 'like', '%'.$search.'%');
})
->where('structure_id' , '=' , '4')
->get();
I have this SQL query that I would like to convert to Eloquent:
$query = "(SELECT name, Heading, 'msg' as type FROM tbl_cms WHERE name LIKE '%" .
$keyword . "%' OR Heading LIKE '%" . $keyword ."%')
UNION
(SELECT name, channel, 'topic' as type FROM tbl_dramas WHERE name LIKE '%" .
$keyword . "%' OR channel LIKE '%" . $keyword ."%')";
My current Eloquent query looks like this:
$keyword = $request->get("term");
if ($keyword != '') {
$query->where(function ($query) use ($request) {
$query->where("name", "LIKE","%$keyword%")
->orWhere("Heading", "LIKE", "%$keyword%");
});
}
return view('search', ['title' => 'search' ,'query' => $query]);
How can I convert it successfully?
Try this:
$query1 = \DB::table('tbl_cms')
->select(\DB::raw('name, Heading, msg AS type'))
->where('name', 'LIKE', '%'.$keyword.'%')
->orWhere('Heading', 'LIKE', '%'.$keyword.'%');
$query2 = \DB::table('tbl_dramas')
->select(\DB::raw('name, channel, topic AS type'))
->where('name', 'LIKE', '%'.$keyword.'%')
->orWhere('channel', 'LIKE', '%'.$keyword.'%');
$result = $query1->union($query2)->get();
$first = DB::table('tbl_cms')
->select('name', 'Heading', 'msg as type')
->where('name', 'like', $keyword.'%')
->orwhere('Heading', 'like', $keyword.'%');
$data = DB::table('tbl_dramas')
->select('name', 'channel', 'topic as type')
->where('name', 'like', $keyword.'%')
->orwhere('channel', 'like', $keyword.'%')
->union($first)
->get();
Something like that
$keyword = '%'.\Input::get('q').'%';
$query1 = \DB::table('tbl_cms')
->select(\DB::raw("name, Heading, 'msg' as 'type'"))
->where('name', 'LIKE', $keyword)
->orWhere('Heading', 'LIKE', $keyword);
$query2 = \DB::table('tbl_dramas')
->select(\DB::raw("name, channel, 'topic' as 'type'"))
->where('name', 'LIKE', $keyword)
->orWhere('channel', 'LIKE', $keyword);
$results = $query1->union($query2)->get();