In Laravel 4 I have built a search form. Where a person can submit either
email address
From - To date
Ref number
They can only search using one of the above options; As they have to select from a drop down on which they would like to search with. The final code I used to get this working is as follows:
if(Input::get('from') && Input::get('to')){
$records = Applications::where('created_at', '>=', Input::get('from'))
->where('created_at', '<', date('Y-m-d', strtotime(Input::get('to'). ' + 1 days')))
->lists('id');
}elseif(Input::get('email') || Input::get('ref')){
$records = Applications::where('Application_number', '=', Input::get('ref'))
->where('email', '=', Input::get('email'), 'OR')
->lists('id');
}else{
$records = Applications::all()->lists('id');
}
The works for my requirements, but I'm not really happy this is the best solution.
I also tried to use either of the solutions in this thread Laravel 4 eloquent WHERE with OR AND OR?
$result = User::where('created_at', 'LIKE', '%' . $search . '%')
->where('updated_at', 'LIKE', '%' . $search . '%', 'OR')
->where('user_first_name', 'LIKE', '%' . $search . '%', 'AND')
->where('user_any_field', 'LIKE', '%' . $search . '%')->get();
OR
Model::where(function ($query) {
$query->where('a', '=', 1)
->orWhere('b', '=', 1);
})->where(function ($query) {
$query->where('c', '=', 1)
->orWhere('d', '=', 1);
});
But all I kept getting was value can't be null, when searching on from and to; email and ref worked fine.
You can try this:
$input = Input::all();
Model::where(function($query) use ($input) {
if (isset($input['something'])) {
$query->where('some_field_1', '=', $input['something']);
}
if (isset($input['something_else'])) {
$query->where('some_field_2', '=', $input['something_else']);
}
})->get();
I think this is the cleanest way. Of course, you should put the if statements to be of your need.
Related
hello guys i want result like i have many products and in products have many supplier and one customer so when i search product description than display all product but i want output like product display but match with customer id
$pi = Product::where('customer_id',$customerId)
->whereHas('supplier', function (Builder $query) use($search) {
$query->where('sys_state', '!=', '-1')
->orWhere('name','LIKE','%' . $search . '%');
})
->where('sys_state','!=','-1')
->orWhere('prd_our_item_no', 'LIKE', '%' . $search . '%')
->orWhere('prd_supplier_item', 'LIKE', '%' . $search . '%')
->orWhere('prd_description','LIKE','%' . $search . '%')
->get();
i try this query but all the product display when i search customer id where condition not work
i try to wrap orwhere but noting can search on it
$pi = Product::where('customer_id', $customerId)
->where('sys_state', '!=', '-1')
->whereHas('supplier', function (Builder $query) use ($search) {
$query->where('sys_state', '!=', '-1')
->orWhere(function ($query) use ($search) {
$query->where('name', 'LIKE', '%' . $search . '%')
->where('sys_state','!=','-1');
});
})
->orWhere(function ($query) use ($search, $customerId) {
$query->where('prd_description', 'LIKE', '%' . $search . '%')
->where('sys_state', '!=', '-1')
->where('customer_id',$customerId);
})
->orWhere(function($query) use($search, $customerId){
$query->where('prd_supplier_item', 'LIKE', '%' . $search . '%')
->where('sys_state', '!=', '-1')
->where('customer_id',$customerId);
})
->orWhere(function($query) use($search, $customerId){
$query->where('prd_our_item_no', 'LIKE', '%' . $search . '%')
->where('sys_state', '!=', '-1')
->where('customer_id',$customerId);
})
->get();
I want to filter one value or multiple values in the Laravel controller
$news_paper_machine_ads = newsPaperMachineAds::join('news_paper_district', 'news_paper_district.idnews_paper_district', '=', 'news_paper_machine_ads.news_paper_district_id')
->join('news_paper_city', 'news_paper_city.idnews_paper_city', '=', 'news_paper_machine_ads.news_paper_city_id')
->join('news_paper_machine_field', 'news_paper_machine_field.idnews_paper_machine_field', '=', 'news_paper_machine_ads.news_paper_machine_field_id')
->where('district', 'LIKE', '%' . $district . '%')
->where('news_paper_city', 'LIKE', '%' . $city . '%')
->where('machine_field', 'LIKE', '%' . $machine_field .'%')
->get();
I want to filter this query past district value(only district data)
city = only city data
machine field = only machine field data
pass district and city = only district and city data
pass district and machine field= only district and machine field data
pass district, city and machine field = only district, city and machine field data
Please help me solve this problem.
$news_paper_machine_ads = newsPaperMachineAds::join('news_paper_district', 'news_paper_district.idnews_paper_district', '=', 'news_paper_machine_ads.news_paper_district_id')
->join('news_paper_city', 'news_paper_city.idnews_paper_city', '=', 'news_paper_machine_ads.news_paper_city_id')
->join('news_paper_machine_field', 'news_paper_machine_field.idnews_paper_machine_field', '=', 'news_paper_machine_ads.news_paper_machine_field_id')
->where(function($query) use($district,$city,$machine_field){
if($district){
$query->where('district', 'LIKE', '%' . $district . '%');
}
if($city){
$query->where('news_paper_city', 'LIKE', '%' . $city . '%');
}
if($machine_field){
$query->where('machine_field', 'LIKE', '%' . $machine_field . '%');
}
})
->get();
use when and OrWhere query like this
$news_paper_machine_ads = newsPaperMachineAds::join('news_paper_district', 'news_paper_district.idnews_paper_district', '=', 'news_paper_machine_ads.news_paper_district_id')
->join('news_paper_city', 'news_paper_city.idnews_paper_city', '=', 'news_paper_machine_ads.news_paper_city_id')
->join('news_paper_machine_field', 'news_paper_machine_field.idnews_paper_machine_field', '=', 'news_paper_machine_ads.news_paper_machine_field_id')
->when($district, function ($query) use ($district) {
return $query->orWhere('district', 'LIKE', '%' . $district . '%');
})
->when($city, function ($query) use ($city) {
return $query->orWhere('news_paper_city', 'LIKE', '%' . $city . '%');
})
->when($machine_field, function ($query) use ($machine_field) {
return $query->orWhere('machine_field', 'LIKE', '%' . $machine_field .'%');
})->get();
$district = $request->district;
$city = $request->city;
$machine_field = $request->machine_field;
$data_arry = array();
if ($district != "Select") {
$data_arry["district"] = $district;
}
if ($city != "Select") {
$data_arry["city"] = $city;
}
if ($machine_field != "Select") {
$data_arry["machine_field"] = $machine_field;
}
$news_paper_machine_ads = newsPaperMachineAds::join('news_paper_district', 'news_paper_district.idnews_paper_district', '=', 'news_paper_machine_ads.news_paper_district_id')
->join('news_paper_city', 'news_paper_city.idnews_paper_city', '=', 'news_paper_machine_ads.news_paper_city_id')
->join('news_paper_machine_field', 'news_paper_machine_field.idnews_paper_machine_field', '=', 'news_paper_machine_ads.news_paper_machine_field_id')
->where(function ($query) use ($data_arry) {
if (isset($data_arry['district'])) {
$query->where('district', $data_arry['district']);
}
if (isset($data_arry['city'])) {
$query->where('news_paper_city', $data_arry['city']);
}
if (isset($data_arry['machine_field'])) {
$query->where('machine_field', $data_arry['machine_field']);
}
})
->get();
I'm trying to figure out how to put generate outer brackets on a Combination of OR statements within a WHEN condition with Laravel Eloquent.
$calls = DB::table('incoming_calls')
->leftJoin('scripts', 'SCRIPTID', '=', 'scripts.RECID')
->select('incoming_calls.RECID','INCOMING_DATE','INCOMING_TIME','SCRIPT_NAME AS MYINFO','MSG_FROM')
->when( (strlen(trim($searchterm))>0), function($query) use ($searchterm)
{
return $query->where('MSG_FROM', 'LIKE', '%'.$searchterm.'%')
->orWhere('MSG_TEL', 'LIKE', '%'.$searchterm.'%')
->orWhere('MSG_MOBILE', 'LIKE', '%'.$searchterm.'%')
->orWhere('MSG_COMPANY', 'LIKE', '%'.$searchterm.'%')
->orWhere('incoming_calls.INFOTXT', 'LIKE', '%'.$searchterm.'%')
->orWhere('MSG_ADDRESS', 'LIKE', '%'.$searchterm.'%')
->orWhere('MSG_EMAIL', 'LIKE', '%'.$searchterm.'%');
})
->when($bid>0, function($query) use ($bid)
{
return $query->where('incoming_calls.COMPANYID', "=", $bid);
})
This is generating the following SQL:
select [incoming_calls].[RECID], [INCOMING_DATE], [INCOMING_TIME], [SCRIPT_NAME] as [MYINFO], [MSG_FROM] from [incoming_calls] left join [scripts] on [SCRIPTID] = [scripts].[RECID] where [MSG_FROM] LIKE ? or [MSG_TEL] LIKE ? or [MSG_MOBILE] LIKE ? or [MSG_COMPANY] LIKE ? or [incoming_calls].[INFOTXT] LIKE ? or [MSG_ADDRESS] LIKE ? or [MSG_EMAIL] LIKE ? and [incoming_calls].[COMPANYID] = ? order by [INCOMING_DATE] desc, [INCOMING_TIME] desc
Now what I need is Brackets around the OR Clauses:
select [incoming_calls].[RECID], [INCOMING_DATE], [INCOMING_TIME], [SCRIPT_NAME] as [MYINFO], [MSG_FROM] from [incoming_calls] left join [scripts] on [SCRIPTID] = [scripts].[RECID] where ( [MSG_FROM] LIKE ? or [MSG_TEL] LIKE ? or [MSG_MOBILE] LIKE ? or [MSG_COMPANY] LIKE ? or [incoming_calls].[INFOTXT] LIKE ? or [MSG_ADDRESS] LIKE ? or [MSG_EMAIL] LIKE ?) and [incoming_calls].[COMPANYID] = ? order by [INCOMING_DATE] desc, [INCOMING_TIME] desc
I understand how to do closures stand-alone and could write this using Raw SQL but I;m wanting to figure how do it with Eloquent within the ->when conditionals. Anyone managed to achieve this?
You can do it via parameter grouping like this:
$calls = DB::table('incoming_calls')
->leftJoin('scripts', 'SCRIPTID', '=', 'scripts.RECID')
->select('incoming_calls.RECID','INCOMING_DATE','INCOMING_TIME','SCRIPT_NAME AS MYINFO','MSG_FROM')
->when( (strlen(trim($searchterm))>0), function($query) use ($searchterm) {
return $query->where(function ($query) {
$query->where('MSG_FROM', 'LIKE', '%' . $searchterm . '%')
->orWhere('MSG_TEL', 'LIKE', '%' . $searchterm . '%')
->orWhere('MSG_MOBILE', 'LIKE', '%' . $searchterm . '%')
->orWhere('MSG_COMPANY', 'LIKE', '%' . $searchterm . '%')
->orWhere('incoming_calls.INFOTXT', 'LIKE', '%' . $searchterm . '%')
->orWhere('MSG_ADDRESS', 'LIKE', '%' . $searchterm . '%')
->orWhere('MSG_EMAIL', 'LIKE', '%' . $searchterm . '%');
})
})
->when($bid>0, function($query) use ($bid)
{
return $query->where('incoming_calls.COMPANYID', "=", $bid);
})
you can do this with one where combined with multiple orwhere
$calls = DB::table('incoming_calls')
->leftJoin('scripts', 'SCRIPTID', '=', 'scripts.RECID')
->select('incoming_calls.RECID','INCOMING_DATE','INCOMING_TIME','SCRIPT_NAME AS MYINFO','MSG_FROM')
->when( (strlen(trim($searchterm))>0), function($query) use ($searchterm)
{
return $query->where(funtion($query) use ($searchterm){
$query->where('MSG_FROM', 'LIKE', '%'.$searchterm.'%')
->orWhere('MSG_TEL', 'LIKE', '%'.$searchterm.'%')
->orWhere('MSG_MOBILE', 'LIKE', '%'.$searchterm.'%')
->orWhere('MSG_COMPANY', 'LIKE', '%'.$searchterm.'%')
->orWhere('incoming_calls.INFOTXT', 'LIKE', '%'.$searchterm.'%')
->orWhere('MSG_ADDRESS', 'LIKE', '%'.$searchterm.'%')
->orWhere('MSG_EMAIL', 'LIKE', '%'.$searchterm.'%');
})
})
->when($bid>0, function($query) use ($bid)
{
return $query->where('incoming_calls.COMPANYID', "=", $bid);
})
Maybe I am doing this incorrectly, however what I want to do logically is this:
I want to first select all posts that contain a certain string.
Then from those results, I want to narrow it down to only posts within a certain date (Y|m|d).
So this is what im doing at the moment:
$results = DB::table('posts');
if ($query == null){
$query = "Posted On: " . $date;
}else {
$results->where('posts.title', 'LIKE', "%$query%")
->orWhere('posts.meta_description', 'LIKE', "%$query%")
->orWhere('posts.meta_keywords', 'LIKE', "%$query%")
->orWhere('posts.body', 'LIKE', "%$query%")
->orWhere('posts.seo_title', 'LIKE', "%$query%");
$query = $query." / Posted On: " . $date;
}
$results->where(function($query) use ($date){
$date = explode('-', $date);
if (!isset($date[1])){
$query->whereYear('created_at', '=', $date[0]);
}else if(isset($date[1]) && !isset($date[2])){
$query->whereYear('created_at', '=', $date[0])
->whereMonth('created_at', '=', $date[1]);
}else if (isset($date[1]) && isset($date[2])){
$query->whereYear('created_at', '=', $date[0])
->whereMonth('created_at', '=', $date[1])
->whereDay('created_at', '=', $date[2]);
}
});
$results = $results->paginate($this->paginate_size());
However, this query is still just returning anything that contains the query string and ignoring the fact that I want to select a subset that are only posts on a specific year/month/day.
What am I missing?
Edit:
Using $results->toSql(); I get the query:
select * from `posts` where `posts`.`title` LIKE ? or
`posts`.`meta_description` LIKE ? or `posts`.`meta_keywords` LIKE ? or
`posts`.`body` LIKE ? or `posts`.`seo_title` LIKE ? and year(`created_at`) = ?
However, it is not working...
If I enter for example the query "Lorem" it will find all of the posts with the text Lorem in it.
However, when I add a specific date like just "2015", it should not show ANY posts from 2015 with the query Lorem since it does not exist.
So there is some logic problem here. Any ideas?
You shouldn't need a subquery.
If you have the proper query in place you should be able to access in the same query.
Of course you can have the dynamic where with the conditions, but do it on the same query. Something along the lines, for example:
if ($query == null){
$query = "Posted On: " . $date;
}
$results->where('posts.title', 'LIKE', "%$query%")
->orWhere('posts.meta_description', 'LIKE', "%$query%")
->orWhere('posts.meta_keywords', 'LIKE', "%$query%")
->orWhere('posts.body', 'LIKE', "%$query%")
->orWhere('posts.seo_title', 'LIKE', "%$query%");
$date = explode('-', $date);
if (!isset($date[1])){
$results->whereYear('created_at', '=', $date[0]);
}else if(isset($date[1]) && !isset($date[2])){
$results->whereYear('created_at', '=', $date[0])
->whereMonth('created_at', '=', $date[1]);
}else if (isset($date[1]) && isset($date[2])){
$results->whereYear('created_at', '=', $date[0])
->whereMonth('created_at', '=', $date[1])
->whereDay('created_at', '=', $date[2]);
}
$results = $results->paginate($this->paginate_size());
You can use the whereDate() Eloquent method:
$results = DB::table('posts');
if ($query == null){
$query = "Posted On: " . $date;
}else {
$results->where(function($q) {
$q->where('posts.title', 'LIKE', "%$query%")
->orWhere('posts.meta_description', 'LIKE', "%$query%")
->orWhere('posts.meta_keywords', 'LIKE', "%$query%")
->orWhere('posts.body', 'LIKE', "%$query%")
->orWhere('posts.seo_title', 'LIKE', "%$query%");
})->whereDate('created_at', '=', date('Y-m-d');
$query = $query." / Posted On: " . $date;
}
$results = $results->paginate($this->paginate_size());
Could someone explain me the reason why is this query not working? I am trying to create a query - where clients name, email or phone match the necessary criteria. However, it seems to work only with one criteria (name or email or phone), not with several ones. That way, I do not get the results needed and I am left with non- filtered list.
Using Laravel 4.1
$q = new Contract;
$q = $q->with(array('lsct' => function($q) {
return $q->select(array('id', 'code'));
}, 'client' => function($q) {
return $q->select(array('id', 'name', 'phone', 'email'));
}));
if(Input::has('search')) {
$criterion = Input::get('search');
$q = $q->where(function($q) use ($criterion) {
$q->where('uid', 'like', '%' . $criterion . '%')->orWhere('car_number', 'like', '%' . $criterion . '%');
})->orWhereHas('lsct', function($q) use($criterion) {
$q->where('code', 'like', '%' . $criterion . '%');
})->orWhereHas('client', function($q) use($criterion) {
$q->where('name', 'like', '%' . $criterion . '%');
// When i try to add condition below, this 'orWhereHas' not working at all
// ->orWhere('phone', 'like', '%' . $criterion . '%')
//->orWhere('latakko_id', 'like', '%' . $criterion . '%');
});
}
Have you tried installing debugbar to show you exactly what your application is querying?
Here is the link for Laravel 4
This might give you some clues if you can actually see what is being queried.