Laravel concat query return 0 items - database

Here is my controller code:
if ($request->exists('tipo')) {
$valor = $request->value;
$candidates = Candidate::buscarpor($tipo, $valor)
->orderBy('id', 'desc')
->Paginate(5)
->withQueryString();
dd($candidates);
}
And this is the scope "buscarpor" inside my "Candidate" model:
public function scopeBuscarpor($query, $tipo, $valor)
{
if(($tipo) && ($valor)) {
if($tipo == 'names') {
// return $query->orWhereRaw("concat(name, ' ', last_name) like '%".$valor."%' ");
return $query->where(DB::raw("CONCAT('name', ' ', 'last_name')"), 'like', '%'.$valor.'%')
->orWhere(DB::raw("CONCAT('last_name', ' ', 'name')"), 'like', '%'.$valor.'%');
}
return $query->where($tipo, 'like', "%$valor%");
}
}
When the search is of type "names" I should query in the DB to search a candidate/person by using his first name or last name, I only have one input type text, I just writting all his names is this input type text.
The variable $valor inside of this scope has data and no problem with it.. I tested adding a name that exists in my database but it returns 0 items.
This my dd($candidates) output.
I don't know what I'm doing wrong, please guys if you have some idea about how fix this problem, I will appreciate it.. Thanks so much.

Maybe the where expressions are not correctly formed. Try writing it like this
$query->where(DB::raw("CONCAT('name', ' ', 'last_name') like '%?%'", [$valor]))
->orWhere(DB::raw("CONCAT('last_name', ' ', 'name') like '%?%'", [$valor]));
You can also use the whereRaw() syntax instead
$query->whereRaw("CONCAT('name', ' ', 'last_name') like '%?%'", [$valor])
->orWhereRaw("CONCAT('last_name', ' ', 'name') like '%?%'", [$valor]);
Or maybe the names are written with capital letters in the database? If you are using postgreSQL, you can use ilike instead of like for a case insensitive search.
If you're using MySQL, you might have to use lower() in your raw methods.
$query->whereRaw("LOWER(CONCAT('name', ' ', 'last_name')) like '%?%'", [$valor])
->orWhereRaw("LOWER(CONCAT('last_name', ' ', 'name')) like '%?%'", [$valor]);

Related

How to Write SOQL for Distinct of combination of Level_1__c,Level_2__c and Level_3__c

I would like to know if there is possibility of writing a SOQL similar like we do in SQL
like Distinct of Level_1__c,Level_2__c and Level_3__c
Currently SOQL is
select Case__c, Level_1__c, Level_2__c,Level_3__c FROM ERT_Case_Type__c
How to rewrite to include Distinct of Level_1__c,Level_2__c and Level_3__c
Thanks & Regards,
Carolyn
I think for your use case, you will need Apex. Something like the following:
Map<String, Set<String>> distinctLevels = new Map<String, Set<String>>{
'Level_1__c' => new Set<String>(),
'Level_2__c' => new Set<String>(),
'Level_3__c' => new Set<String>()
};
Set<String> levelApiNames = new Set<String>{'Level_1__c', 'Level_2__c', 'Level_3__c'};
for(ERT_Case_Type__c caseType : [SELECT Case__c, Level_1__c, Level_2__c, Level_3__c FROM ERT_Case_Type__c]){
for(String level : levelApiNames){
distinctLevels.get(level).add(caseType.get(level));
}
}
for(String level : levelApiNames){
System.debug('Number of distinct ' + level + ' values: ' + String.valueOf(distinctLevels.get(level).size());
//System.debug('List of distinct ' + level + ' values: ' + distinctLevels.get(level).toString());
//System.debug(distinctLevels.get(level).size()); //as integer
}
Loop over query results
Loop over each field API name you want to track
Add the values for each field to the set inside the map. The set will only have unique values
Do something with the map values for each field API name
This is not tested, but the high-level outline should work for you.

Pass loop value along with a string

I want to move among the sheets within the same workbook, my sheet names are like 'Order 1', 'Order 2', 'Order 3'........so on. I want to take the value from loop along with 'Order' string like 'Order [i]' and want to paste some formula that also takes some value from the loop like ={Link!B[[i]+2]}
I tried with this following code but can't be succeeded.
function Order() {
var spreadsheet = SpreadsheetApp.getActive();
for (var i = 1; i <= 10; i++) {
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Order [i]'));
spreadsheet.getRange('B3').activate();
spreadsheet.getCurrentCell().setValue('={Link!B[[i]+2]}');;
}
};
When you specify 'Order [i]', your script looks for a sheet called "Order [i]". Instead, you should write 'Order ' + i.
Similarly, '={Link!B[[i]+2]}' will return "={Link!B[[i]+2]}", just as you wrote it. Instead, write '={Link!B' + (i+2) + '}'.
Also, you don't need the .activate() or .setActiveSheet() calls. You can simply get the range and then immediately set the value. You're getting the sheet already with the .getSheetByName() method. In fact, you link all of it together.
function Order() {
var spreadsheet = SpreadsheetApp.getActive();
for (var i = 1; i <= 10; i++) {
spreadsheet.getSheetByName('Order ' + i)
.getRange('B3')
.setValue('={Link!B' + (i+2) + '}');
}
};
The string 'Order [i]' will always just be 'Order [i]', as everything is wrapped in quotes so the computer is interpreting it literally. You want to use something like 'Order ' + i, which will evaluate what i is and then append it to 'Order'.
In the second example, you might want something like '={Link!' + B[i+2] + '}'

Find first instance of space in string

I am trying to return the first 'word' in a string by finding the first instance of a space ' ' in the string field Part_Comment. Examples of strings in the field Part_Comment are:
13088V21 () (FAB)
G16707 (FOLD) ()
16636U01.01
I have tried:
substring(Part_Comment from 1 for position(' ' in Part_Comment)-2) as "AssyNo",
which comes up with an error "Incorrect syntax near the keyword 'from'." But it works fine when I just use Part_Comment by itself.
substring(Part_Comment from 1) as "AssyNo",
Same error as above
left(Part_Comment,10) as "AssyNo",
This works, but I need to use the position function or something else to find the ' ' substring. But apparently the position function returns 0 when more than one instance occurs.
I imagine this is a pretty common thing that users want, so there must be an easy solution.
You can do it with LEFT and POSITION like so:
LEFT(Part_Comment, POSITION(' ' in Part_Comment)-1))
EDIT
as #Arioch 'The in comment suggested, safety need to be implemented
CASE POSITION(' ' in Part_Comment)
WHEN 0 THEN 'Part_Comment'
ELSE LEFT(Part_Comment, POSITION(' ' in Part_Comment)-1)
END

Matlab using editbox to match database

I'm facing problem when changing the user input using editbox when retrieve value from database.
The following is the working code.
conn = database('SQL', '', '');
name = input('what is your name: ', 's');
sqlquery = ['select Staff.staffPW from imageProcessing.dbo.Staff '...
'where Staff.staffID = ' '''' name ''''];
curs = exec(conn,sqlquery);
curs = fetch(curs);
curs.Data
close(curs)
close(conn)
But now when I changed the input using editbox, problem occured
function pushbutton1_Callback(hObject, eventdata, handles)
conn = database('SQL', '', '');
name = get(handles.edit1,'String');
sqlquery = ['select Staff.staffPW from imageProcessing.dbo.Staff '...
'where Staff.staffID = ' '''' name ''''];
curs = exec(conn,sqlquery);
curs = fetch(curs);
curs.Data
close(curs)
close(conn)
I can get the correct pw from the working code, but the input from editbox I'm getting nothing. Anyone can teach me how to make it work? Thanks a lot!
The immediate issue is that the String property in your case is a cell array containing a string rather than just a plain string.
MATLAB edit-style uicontrols are capable of displaying multiple lines of text. To fill in multiple lines of text, in these cases, the string can be passed in as a cell array of strings where each element is on a different line of the edit box.
control = uicontrol('style', 'edit', ...
'Max', 2, ... % A two-line edit box
'String', {'Row1', 'Row2'});
Because of this, even for a single-line edit box, the String property could be a cell array of strings or just a string. Therefore, when retrieving values from an edit box, be sure to check if it is a cell array (iscell) or a string (ischar) before using it.
So adapting this to your code, we could do something like the following
name = get(handles.edit1, 'String');
% Check to ensure it is a cell array of one string
if iscell(name) && numel(name) == 1
name = name{1};
end
% Disallow non-strings, cell arrays of multiple strings, or empty strings
if ~ischar(name) || isempty(name)
error('A valid string must be supplied!');
end
sqlquery = ['select Staff.staffPW from imageProcessing.dbo.Staff '...
'where Staff.staffID = ' '''' name ''''];

$this->db->where how to use it?

In this code, I want to select the records from database only where the employee_id_open and the employee_id_close is the same and is same to a variable $employee_id
$employee_id = 1;
$between = 'between ' . $this->db->escape($this->params['start_date'] . ' 00:00:00').' and ' . $this->db->escape($this->params['end_date'] . ' 23:59:59');
$this->db->select("registers.name as register_name, open_person.first_name as open_first_name, open_person.last_name as open_last_name,open_person.hourly_salary as hourly_salary, close_person.first_name as close_first_name, close_person.last_name as close_last_name, register_log.*, (register_log.close_amount - register_log.open_amount - register_log.cash_sales_amount) as difference");
$this->db->from('register_log as register_log');
$this->db->join('people as open_person', 'register_log.employee_id_open=open_person.person_id');
$this->db->join('people as close_person', 'register_log.employee_id_close=close_person.person_id', 'left');
$this->db->join('registers', 'registers.register_id = register_log.register_id');
//------My modification - but this is not working. no records are selected with this query
$this->db->where('open_person.person_id ', 'close_person.person_id');
// I also tried this>>> $this->db->where('open_person.person_id = ', 'close_person.person_id');
//------/ My modifcation
$this->db->where('register_log.shift_start ' . $between);
$this->db->where('register_log.deleted ', 0);
$this->db->where('registers.location_id', $location_id);
Can you please tell me what I am doing wrong and how can I fix this?
Any help would be much appreciated.
Many Thanks
you can dump the last query sql statement using $this->db->last_query().

Resources