I am trying to implement a search feature in a site. A user can specify a search string that could match a product_id, product_name or the product_sku. What I want to implement is a search function that would return all that matches.
What I've come with so far:
return Product::with(['getProductItem' => fn ($query) => $query->orWhere('itemSkuNumber', '=', $request->search)])
->where(
'name',
$request->has('match') ? '=' : 'like',
$request->has('match') ? $request->search : '%' . $request->search . '%'
)
->orWhere('id', '=', $request->search)->get();
This has worked if I specify a product_id or product_name but does not return anything if I provide an sku.
You should wrap orWheres with another where because it will try to filter all of them.
Product::with(['getProductItem'])
->whereHas('getProductItem', function ($query) use ($request) {
$query->where(function ($subquery) use ($request) {
$subquery->where('itemSkuNumber', '=', $request->search);
//put your orwhere queries in here
});
})
->get();
Try whereHas
$products = Product::query()
->with(['getProductItem'])
->whereHas('getProductItem', fn ($query) => $query->where('itemSkuNumber', '=', $request->search)]))
->orWhere(
'name',
$request->has('match') ? '=' : 'like',
$request->has('match') ? $request->search : '%' . $request->search . '%'
)
->orWhere('id', '=', $request->search)
->get();
#gguney, #Sumit,
Thanks for your help, :).
Got it working with:
return Product::with(['getProductItem'])
->whereHas('getProductItem', function ($query) use ($request) {
$query->where('itemSkuNumber', '=', $request->search);
})
->orWhere(
'name',
$request->has('match') ? '=' : 'like',
$request->has('match') ? $request->search : '%' . $request->search . '%'
)
->orWhere('id', '=', $request->search)
->get();
#Sumit,
You have extra parentheses and brackets.
Related
I need to search for the code in the appointment table OR the patient name which is the appointment's relation. here is the code I reached so far but it is not working:
$lab = Lab::with(['patient' => function ($q) use ($search_query) {
$q->select('id', 'avatar', DB::raw('CONCAT(first_Name, " ", second_Name) AS name')
->where('name', 'like', "%{$search_query}%")
->orWhereRaw("concat(first_name, ' ', second_name) like '%$search_query%' ")
);
}])
->select('id', 'code')
->Where('code', 'like', "%{$search_query}%")
->limit(5)
->get();
Loding relation is different from applying 'where' on it, you sould load the relation and apply 'whereHas' in another statement:
$lab = Lab::with(['patient' => function ($q) {
$q->select('id', 'avatar', DB::raw('CONCAT(first_Name, " ", second_Name) AS name')
);
}])
->select('id', 'code')
->Where('code', 'like', "%{$search_query}%")
->orWhereHas('patient',function ($query) use ($search_query) {
$query->where('name', 'like', "%{$search_query}%");
})
->limit(5)
->get();
I am trying to create an items filter based on user selections. The filter works with GET and fetches it's parameters from the url.
I tried using the laravel's when function but the filter is not working, it keeps returning the same result set without adhering to the filtration rule I wrote for it.
I am on Laravel 8 and I used the method outlined on Laravel's Conditional Clauses.
I feel like the problem is with the whereBetween clause between even after hard coding values into it, the result was the same as before.
Any help to fix this is greatly appreciated in advance.
$queryString = $request->query();
$sortBy = null;
$minMaxArray = array();
if($queryString){
if (array_key_exists('sortby', $queryString)) {
$sortBy = ($queryString['sortby'] == 'lowest') ? 'ASC' : 'DESC';
}
if (array_key_exists('min', $queryString) || array_key_exists('max', $queryString)) {
$minMaxArray['min'] = preg_replace("/,/", '', $queryString['min']) ?: 0;
$minMaxArray['max'] = preg_replace("/,/", '', $queryString['max']) ?: 1000000;
}
}
$tutors = DB::table('users')
->join('tutors', 'users.email', '=', 'tutors.tutorEmail')
->where([
['users.name', 'like', '%' . trim($searchText) . '%'],
['tutors.tutorStrength', '<>', '-'],
['users.status', 'active'],
['users.gender', '<>', '-'],
['users.phone', '<>', '-'],
['users.role', 'tutor']
])
->orWhere([
['tutors.tutorStrength', 'like', '%' . trim($searchText) . '%'],
['users.status', 'active'],
['users.gender', '<>', '-'],
['users.phone', '<>', '-'],
['users.role', 'tutor'],
])
->orWhere([
['users.location', 'like', '%' . trim($searchText) . '%'],
['tutors.tutorStrength', '<>', '-'],
['users.status', 'active'],
['users.gender', '<>', '-'],
['users.phone', '<>', '-'],
['users.role', 'tutor'],
])
->when($minMaxArray, function ($subQuery) use ($minMaxArray){
return $subQuery->whereBetween('tutors.tutorFee', $minMaxArray);
})
->when($sortBy, function ($subQuery) use ($sortBy){
return $subQuery->orderBy('tutors.tutorFee', $sortBy);
}, function ($subQuery){
return $subQuery->inRandomOrder();
})
->select('tutors.tutorFee', 'tutors.feeType', 'tutors.tutorOccupation', 'tutors.tutorStrength', 'users.*')->distinct()->paginate(20);
I fixed it. Turned out that I needed to duplicate the when conditional clauses on each where and orWhere clause.
I appreciate Kamlesh Paul for trying to help.
I use this : https://github.com/rinvex/repository
My query is like this :
$query = $this->store_repository
->join('favorites', function ($join) {
$join->on('stores.id', '=', 'favorites.favoritable_id')
->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store');
})
->where('stores.status', '=', 1)
->select('stores.id', 'stores.name', 'stores.photo','stores.address');
if($location)
$query->where('stores.address', 'like', "%$location%");
if($q) {
$query->where('stores.name', 'like', "%$q%")
->where('stores.address', 'like', "%$q%", 'or');
}
$result = $query->orderBy('favorites.updated_at', 'desc')->paginate($num);
When executed, there exist error like this :
Missing argument 3 for
Rinvex\Repository\Repositories\BaseRepository::join()
How can I solve it?
I don't think you need the clojure to make what you want.
I guess this should be fine :
$query = $this->store_repository
->join('favorites', '=', 'favorites.favoritable_id')
->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store');
->where('stores.status', '=', 1)
->select('stores.id', 'stores.name', 'stores.photo','stores.address');
I am trying to build a search form with laravel, but I cannot get the where clause to work.
$term = $request->input('term');
$count = DB::table('members as m')
->where(DB::raw('m.member_first_name'), 'LIKE', "%$term%")
->orWhere(DB::raw('m.member_last_name'), 'LIKE', "%$term%")
->orWhere(DB::raw('m.member_business_address'), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_first_name',' ','m.member_last_name')"), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_last_name',' ','m.member_first_name')"), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_name_affix',' ','m.member_last_name',' ','m.member_first_name')"), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_first_name',' ','m.member_name_affix',' ','m.member_last_name')"), 'LIKE', "%$term%")
->count();
var_dump($count);
var_dump($count) always returns all the database entries, no matter what the search term is.
This is my first Laravel project and I would be very thankful for any kind of help.
Perhaps try encasing the query in an overall where:
$count = DB::table('members as m')
->where(function ($q) {
$q->where(DB::raw('m.member_first_name'), 'LIKE', "%$term%")
->orWhere(DB::raw('m.member_last_name'), 'LIKE', "%$term%")
->orWhere(DB::raw('m.member_business_address'), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_first_name',' ','m.member_last_name')"), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_last_name',' ','m.member_first_name')"), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_name_affix',' ','m.member_last_name',' ','m.member_first_name')"), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_first_name',' ','m.member_name_affix',' ','m.member_last_name')"), 'LIKE', "%$term%")
})
->count();
You're using and / or together, which of course will fail. Try the following:
$count = DB::table('members as m')
->where(DB::raw('m.member_first_name'), 'LIKE', "%$term%")
->where(function ($query) {
$query->where(DB::raw('m.member_last_name'), 'LIKE', "%$term%")
->orWhere(DB::raw('m.member_business_address'), 'LIKE', "%$term%"));
})
Simply try this code and if you got some syntax error then try to change the ' symbols here and there..as I can't test it before post..
$term = "%".$request->input('term')."%";
$count = DB::table('members as m')
->where('m.member_first_name', 'LIKE', $term)
->orWhere('m.member_last_name', 'LIKE', $term)
->orWhere('m.member_business_address', 'LIKE', $term)
->orWhereRaw('(concat_ws(m.member_first_name,m.member_last_name) LIKE ? )',[$term])
->orWhereRaw('(concat_ws(m.member_last_name,m.member_first_name) LIKE ? )',[$term])
->orWhereRaw('(concat_ws(m.member_name_affix,m.member_last_name,m.member_first_name) LIKE ? )',[$term])
->orWhereRaw('(concat_ws(m.member_first_name,m.member_name_affix,m.member_last_name) LIKE ? )',[$term])
->count();
var_dump($count);
you can use whereRaw() don't need to put DB::raw() inside where().
I want do following query in laravel elequent
select from table where cond1 and(cond or cond or cond..)and cond3.
I have used following code,
$invoice = Invoice::with("items", "salesPerson", "client");
if ($q = Request::input("q")) {
$invoice->where("invoices.invoice_number", 'LIKE', "%$q%");
$invoice->orWhere("invoices.reference_number", 'LIKE', "%$q%");
$invoice->orWhere("invoices.client_name", 'LIKE', "%$q%");
$invoice->orWhere("invoices.invoice_date", 'LIKE', "$q%");
$invoice->orWhere("invoices.due_date", 'LIKE', "$q%");
}
$invoice->whereNull("invoices.deleted_at");
but it return deleted records as well.How can I fix this?
In this case a nested where comes in handy:
$invoice = Invoice::with("items", "salesPerson", "client");
if ($q = Request::input("q")) {
$invoice->where(function($query) use ($q){
$query->where("invoices.invoice_number", 'LIKE', "%$q%");
$query->orWhere("invoices.reference_number", 'LIKE', "%$q%");
$query->orWhere("invoices.client_name", 'LIKE', "%$q%");
$query->orWhere("invoices.invoice_date", 'LIKE', "$q%");
$query->orWhere("invoices.due_date", 'LIKE', "$q%");
});
}
$invoice->whereNull("invoices.deleted_at");