Im working on product filtering using AJAX. Is there any possible way to produce same output as picture shown below using query builder?
I have tried union but it’s not working.
I hope this example gives idea , Try this one
use multiple if statement and get data into DB using joins .
function datatables($request) {
$data = $this->leftJoin('blog_category', 'blog_category.blog_category_uuid', '=', 'blog_detail.blog_category_uuid')
->where('blog_detail.blog_detail_is_deleted', 'NO');
if ($request->search['value'] != null && $request->search['value'] != '') {
$keyword = $request->search['value'];
$data = $data->where(function($query) use ($keyword) {
// $query->orWhere('activity_type.activity_type_name', 'LIKE', '%' . $keyword . '%');
$query->orWhere('blog_detail.blog_detail_title', 'LIKE', '%' . $keyword . '%');
});
}
if (isset($request->order[0]['dir'])) {
$data = $data->orderBy('blog_detail.blog_detail_id', $request->order[0]['dir']);
} else {
$data = $data->orderBy('blog_detail.blog_detail_created_date');
}
$datacount = $data->count();
$dataArray = $data->select('blog_detail.*', 'blog_category.blog_category_name' , DB::raw('DATE_FORMAT(blog_detail.blog_detail_created_date,"%Y-%m-%d") as blog_detail_date'));
if ($request->length == -1) {
$dataArray = $dataArray->get();
} else {
$dataArray = $dataArray->skip($request->start)->take($request->length)->get();
}
return [$datacount, $dataArray];
}
In laravel you can create a model for product say Product. Then the query will be like
$products = Product::where('product_status', '1');
if ($request->input('minimum_price') && $request->input('maximum_prize')) {
$products = $products->whereBetween('product_prize', array($request->input('minimum_price'), $request->input('maximum_prize')));
}
if ($request->input('brand')){
$brand_filter = implode("','", $request->input('brand'));
$products = $products->whereIn('product_brand', $brand_filter);
}
$products = $products->get();
after the execution $products contains the products after query.
Related
i want to create a multiple search option but it gives me error when i try to search
this is my code by the way
Controller
$leaves = DB::table('leaves_admin')
->join('users','users.rec_id', '=', 'leaves_admin.rec_id')
->select('leaves_admin.*','users.role_name','users.avatar')
->get();
$userList = DB::table('users')->get();
// search by name
$search = request('name');
$leaves = LeaveAdmin::join('users','users.rec_id','=', 'leaves_admin.rec_id')
->select('leaves_admin.*','users.avatar')
-> where('leaves_admin.name','LIKE','%'.$search.'%')
->get();
// search by status
$search = request('status');
$leaves = LeaveAdmin::join('users','users.rec_id','=', 'leaves_admin.rec_id')
->select('leaves_admin.*','users.avatar')
-> where('leaves_admin.status','LIKE','%'.$search.'%')
->get();
return view('Leave.leaves', compact('leaves'));
As per my understanding of your code that you are looking for a solution for multiple filters and I am assuming that all columns are present in the same Query, so I will promote that solution for you.
$name = (request('name')) ? request('name') :'';
$status = (request('status')) ? request('status') :'';
$query = DB::table('leaves_admin')
->join('users','users.rec_id', '=', 'leaves_admin.rec_id')
->select('leaves_admin.*','users.role_name','users.avatar');
if(!empty($name) && !empty($status) ) {
$query->where('leaves_admin.name','LIKE','%'.$name.'%');
$query->where('leaves_admin.status','LIKE','%'.$status.'%');
}elseif (!empty($name)) {
$query->where('leaves_admin.name','LIKE','%'.$name.'%');
}elseif (!empty($status)) {
$query->where('leaves_admin.status','LIKE','%'.$status.'%');
}
$leaves = $query->get()->toArray();
return view('Leave.leaves', compact('leaves'));
I want to remove where clauses in some conditions:
$q = Thread::with('comments')->take(10);
if(strlen($input) < 4){
$result = $q->where('title', '~', "^$input$")->get();
if($result->isNotEmpty()){
return $result;
}
// if was empty:
}
// How to clean the where clause from the above here? because it affect the query below:
$result = $q->where('title', 'like', "%$input%")->get();
The problem is the first where clause affects the second one if the data was empty, How can i remove existing where clauses when needed ? also the newQuery() is not working in my case.
Note that i'm using two seperate statement in postgres ~ and 'like'
Something like reorder() for where clauses
Yes there is a way to do it
$q = Thread::with('comments')->take(10);
if(strlen($input) < 4){
$result = $q->where('title', '~', "^$input$")->get();
if($result->isNotEmpty()){
return $results;
}
}
// getQuery() is a query builder method that contains all the groupings, selects, orders, wheres, joins etc for the query that you are accessing or trying to build.
$q->getQuery()->wheres= [];
$result = $q->where('title', 'like', "%$input%")->get();
Use Conditional Clauses
$threads = Thread::with('comments')->when(strlen($input) < 4, function ($query) use ($input) {
return $query->where('title', '~', "^$input$");
}, function ($query) use ($input) {
return $query->where('title', 'like', "%$input%");
})->take(10)->get();
https://laravel.com/docs/8.x/queries#conditional-clauses
If you want to give it a second change I would write that logic like this. Comments are loaded only if they are needed.
if (strlen($input) < 4) {
$threads = Thread::where('title', '~', "^$input$")->take(10)->get();
if ($threads->isNotEmpty()) {
return $threads->load('comments');
}
}
return Thread::with('comments')->where('title', 'like', "%$input%")->take(10)->get();
if you cloning before add where
it works the same as deleting where
...
if(strlen($input) < 4){
$result = (clone $q)->where('title', '~', "^$input$")->get();
if($result->isNotEmpty()){
return $results;
}
}
$result = $q->where('title', 'like', "%$input%")->get();
...
I have a dB query where I would like to groupBy() only when conditions are met without using union because of pagination.
Unfortunately groupBy() seems to only work when called on the entire query outside of the loop.
This was made for dynamic filtering from $filterArr. Depending on the array I need to select from different columns of the table.
When the $key=='pattern' I would need the distinct results from its column.
the query looks something like this
select `col_1`, `col_2`, `col_3`
from `mytable`
where (`color` LIKE ? or `pattern` LIKE ? or `style` LIKE ?)
group by `col_2` //<< i need this only for 'pattern' above and not the entire query
Heres the model:
// $filterArr example
// Array ( [color] => grey [pattern] => stripe )
$query = DB::table('mytable');
$query = $query->select(array('col_1', 'col_2', 'col_3'), DB::raw('count(*) as total'));
$query = $query->where(function($query) use ($filterArr){
$ii = 0;
foreach ($filterArr as $key => $value) {
if ($key=='color'){
$column = 'color';
}else if ($key=='style'){
$column = 'style';
}else if ($key=='pattern'){
$column = 'pattern';
$query = $query->groupBy('col_2'); // << !! does not work
}
if($ii==0){
$query = $query->where($column, 'LIKE', '%'.$value.'%');
}
else{
$query = $query->orWhere($column, 'LIKE', '%'.$value.'%');
}
$ii++;
}
});
$query = $query->orderBy('col_2', 'asc')->simplePaginate(30);
I think you can simplify your code a bit:
$query = DB::table('mytable');
$query = $query->select(array('col_1', 'col_2', 'col_3'), DB::raw('count(*) as total'));
$query = $query->where(
collect($filterArr)
->only(['color','style','pattern'])
->map(function ($value, $key) {
return [ $key, 'like', '%'.$value.'%', 'OR' ];
})->all()
)->when(array_key_exists('pattern', $filterArr), function ($query) {
return $query->groupBy('col_2');
});
$query = $query->orderBy('col_2', 'asc')->simplePaginate(30);
I have some conditions in array like
$category = Input::get('category');
$cuisine = Input::get('cuisine');
$veg = Input::get('veg');
$trending = Input::get('trending');
$time = Input::get('time');
if($category) $conditions['category'] = $category;
if($cuisine) $conditions['cuisine'] = $cuisine;
if($veg) $conditions['veg'] = $veg;
if($trending) $conditions['trending'] = $trending;
How can I make
$list = Data::where($conditions)->where('cuisine','LIKE','%'.$cuisine.'%')->get();
Is it possible to enter LIKE % in this statement
if($cuisine) $conditions['cuisine'] = $cuisine;
The problem is that if I want to add this where('cuisine','LIKE','%'.$cuisine.'%') several areas it needs to be updated. and in some cases, if cuisine is not present everything cannot be fetched
I want to perform LIKE statement for only cuisine data.
Sure, you can do that by creating an array with this format:
[['column1', 'like', '%' . $filter1 . '%'], ['column2', 'like', '%' . $filter2 . '%']]
For example:
$fields = ['category', 'cuisine', 'veg', 'trending', 'time'];
foreach ($fields as $field) {
if ($request->get($field)) {
$conditions[] = [$field, 'like', '%' . $request->get($field) . '%'];
}
}
$list = Data::where($conditions)->get();
Another example from the docs:
You may also pass an array of conditions to the where function:
$users = DB::table('users')->where([
['status', '=', '1'],
['subscribed', '<>', '1'],
])->get();
https://laravel.com/docs/5.5/queries#where-clauses
Update
You've just updated your question and said you want to use like only for $cuisine. In this case, you can use a closure:
->where(function($q) use($request) {
if ($request->cuisine) {
$q->where('cuisine', 'like', '%' . $request->cuisine . '%');
}
})
Or you could use when():
->when($request->cuisine, function ($q) use ($cuisine) {
return $q->where('cuisine', 'like', '%' . $request->cuisine . '%');
})
Well, you can do it in parts:
$query = Data::where($conditions);
if($cuisine) {
$query->where('cuisine','LIKE','%'.$cuisine.'%');
}
$list = $query->get();
You can do it like,
$query = DB::table('data');
$category = Input::get('category');
$cuisine = Input::get('cuisine');
$veg = Input::get('veg');
$trending = Input::get('trending');
$time = Input::get('time');
if($category) {
$query->where('category','LIKE','%'.$category.'%');
}
if($cuisine) {
$query->where('cuisine','LIKE','%'.$cuisine.'%');
}
if($veg) {
$query->where('veg','LIKE','%'.$veg.'%');
}
if($trending) {
$query->where('trending','LIKE','%'.$trending.'%');
}
if($time) {
$query->where('time','LIKE','%'.$time.'%');
}
$list = $query->get();
I hope you will understand.
Why not just assign as blank value as default as it will pass in LIKE for all cases
$conditions['cuisine']= (isset($cuisine)&&$cuisine)) ? $cuisine : '';
Well, you have to assign query to some variable:
$query = Data::where($conditions);
if($cuisine) {
$query = $query->where('cuisine','LIKE','%'.$cuisine.'%');
}
$list = $query->get();
After some help in a previous post Laravel - Search Facility on site using Eloquent I now need to some help on paginating the result using the built in laravel pagination class.
public function search() //no parameter now
{
$q = Input::get('term');
if($q && $q != ''){
$searchTerms = explode(' ', $q);
$query = DB::table('wc_program'); // it's DB::table(), not DB::tables
if(!empty($searchTerms)){
foreach($searchTerms as $term) {
$query->where('JobRef', 'LIKE', '%'. $term .'%');
}
}
$results = $query->get();
dd($results); // for debugging purpose. Use a View here
}
}
Simply change get to paginate and provide number of items per page.
$results = $query->paginate(10);