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'));
Related
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);
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.
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();
In case I have filter users with location and date,I have written query like below
public function searchConsultants()
{
$location = $request->get('location');
$fromDate = $request->get('from_date');
$toDate = $request->get('to_date');
$data = DB::table('consultants')
->selectRaw('AVG(COALESCE(ratings.rating_for_manager, 0))as avg_rating, consultants.id,consultants.cunsultant_name,contact_number,location')
->where('location','LIKE','%'.$location.'%')
->whereBetween('date',[$fromDate,$toDate])
->leftJoin('ratings', 'ratings.consultant_id', 'consultants.id')
->groupBy('consultants.id')
->orderBy('avg_rating', 'DESC')
->get();
}
With above query I can get data but sometimes I dont want to search with date I want search only with location,
The problem in above query I must enter location and date to filter users,So How can I filter with only location or date.
Use orWhere read it at https://laravel.com/docs/5.4/queries#where-clauses
public function searchConsultants()
{
$location = $request->get('location');
$fromDate = $request->get('from_date');
$toDate = $request->get('to_date');
$data = DB::table('consultants')
->selectRaw('AVG(COALESCE(ratings.rating_for_manager, 0))as avg_rating, consultants.id,consultants.cunsultant_name,contact_number,location')
->where('location','LIKE','%'.$location.'%')
->orwhereBetween('date',[$fromDate,$toDate])
->leftJoin('ratings', 'ratings.consultant_id', 'consultants.id')
->groupBy('consultants.id')
->orderBy('avg_rating', 'DESC')
->get();
}
You should try this:
public function searchConsultants()
{
$location = $request->get('location');
$fromDate = $request->get('from_date');
$toDate = $request->get('to_date');
$data = DB::table('consultants')
->selectRaw('AVG(COALESCE(ratings.rating_for_manager, 0))as avg_rating, consultants.id,consultants.cunsultant_name,contact_number,location')
->whereBetween('date',[$fromDate,$toDate])
->orWhere('location','LIKE','%'.$location.'%')
->leftJoin('ratings', 'ratings.consultant_id', 'consultants.id')
->groupBy('consultants.id')
->orderBy('avg_rating', 'DESC')
->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);