Laravel: Find exact match, case sensitive - sql

This code works but it is not sensitive to case/capitalization.
public function search(){
if ($search = \Request::get('q')) {
$patrons = Patron::where(function($query) use ($search){
$query->where('barcode','=',"$search")
})->paginate(20);
}else{
$patrons = Patron::latest()->paginate(5);
}
return $patrons;
}
Example
Searching for 'banana123' should NOT be equal to 'BaNaNa123' and should return 0.
How can we make the search exact match? Thank you.

You have to use BINARY
use DB;
...
public function search(){
if ($search = \Request::get('q')) {
$patrons = Patron::where(function($query) use ($search){
$query->whereRaw("BINARY `barcode` = '$search'");
// or
// $query->where(DB::raw("BINARY `barcode`), $search);
})->paginate(20);
}else{
$patrons = Patron::latest()->paginate(5);
}
return $patrons;
}
Also if you are using one where and not chaining it to other conditions you don't need to use function inside where. you can simply write:
use DB;
...
public function search(){
if ($search = \Request::get('q')) {
$patrons = Patron::where(DB::raw("BINARY `barcode`"), $search)->paginate(20);
}else{
$patrons = Patron::latest()->paginate(5);
}
return $patrons;
}

Related

Why can't I send the logged in user data to vue using Auth::id()?

Why it doesn't work with Auth::id(), but works with a hardcoded value like 1.
The following doesn't work:
public function getTwitterUser()
{
$auth_id = Auth::id();
$getuser = Twitterac::where('user_id', $auth_id)->get();
return $getuser;
}
But this does:
public function getTwitterUser()
{
$auth_id = 1;
$getuser = Twitterac::where('user_id', $auth_id)->get();
return $getuser;
}

Reduction number of IF in my case

I have too much IF in my method like this:
if (myObject?.name !=null)
first.text = myObject.name.bigThing
if (myObject?.age !=null)
second.text = myObject.age.bigThing
if (myObject?.surname !=null)
third.text = myObject.surname.bigThing
and 20 more ...
How can I shorten the code?
age/surname/name is type my own class Big with id: Int and bigThing: String
One way could be:
myObject?.age?.let { second.text = it.bigThing }
If you're putting the value inside a TextView:
first.text = myObject?.age?.bigThing
One option would be
fun updateText(x: WhateverTheTypeOfFirstSecondEtcIs, y: Big?) {
if (y != null) { x.text = y.bigThing }
}
updateText(first, myObject?.name)
updateText(second, myObject?.age)
updateText(third, myObject?.surname)
Change each of them like this if you like it:
myObject?.name?.run { first.text = this.bigThing }
I don't know the exact syntax in kotlin, however I would expect to be able to create a method something like this:
String extractBigThing(Big big){
if(big != null) return big. GetBigThing() ;
return null;
}
And call it something like this :
first.text = extractBigThing(myObject.name);
The basic idea is to extract reused functionality into reusable code, these can then be unit tested to increase code robustness.
I hope this helps.
myObject?.name?.bigThing?.let { first.text = it } etc for each line
or
myObject?.apply {
name?.bigThing?.let { first.text = it }
age?.bigThing?.let { second.text = it }
}
etc

Prestashop capitalize customer address

I need to capitalize all user data in Prestashop 1.6 - I've managed to get it done for name, etc. but I don't know where I can do this for the address.
My guess is AddressController's processSubmitAddress() method, but I cannot find where does it take the input, so I can strotupper() that. Thanks for any guidance.
A little late, but here it is. Just change the ucfirst for whatever you need, in this case strotupper(). This is for the customer's address. In same file there should be other files for different customer data. This works also for PS 1.7.2 (tested)
Go to yourprojectfolder/classes/Address.php
Search for public function add($autodate = true, $null_values = false) on line 169.
Replace :
public function add($autodate = true, $null_values = false)
{
if (!parent::add($autodate, $null_values)) {
return false;
}
if (Validate::isUnsignedId($this->id_customer)) {
Customer::resetAddressCache($this->id_customer, $this->id);
}
return true;
}
With:
public function add($autodate = true, $null_values = false)
{
if (!parent::add($autodate, $null_values)) {
return false;
}
// Capitalize the first name
$this->firstname = ucfirst($this->firstname);
// Capitalize the first name
$this->lastname = ucfirst($this->lastname);
// Capitalize the address fields
$this->address1 = ucfirst($this->address1);
$this->address2 = ucfirst($this->address2);
if (Validate::isUnsignedId($this->id_customer)) {
Customer::resetAddressCache($this->id_customer, $this->id);
}
return true;
}
Search for public function update($null_values = false) on line 181.
Replace :
public function update($null_values = false)
{
// Empty related caches
if (isset(self::$_idCountries[$this->id])) {
unset(self::$_idCountries[$this->id]);
}
if (isset(self::$_idZones[$this->id])) {
unset(self::$_idZones[$this->id]);
}
if (Validate::isUnsignedId($this->id_customer)) {
Customer::resetAddressCache($this->id_customer, $this->id);
}
return parent::update($null_values);
}
With:
public function update($null_values = false)
{
// Empty related caches
if (isset(self::$_idCountries[$this->id])) {
unset(self::$_idCountries[$this->id]);
}
if (isset(self::$_idZones[$this->id])) {
unset(self::$_idZones[$this->id]);
}
// Capitalize the first name
$this->firstname = ucfirst($this->firstname);
// Capitalize the first name
$this->lastname = ucfirst($this->lastname);
// Capitalize the address fields
$this->address1 = ucfirst($this->address1);
$this->address2 = ucfirst($this->address2);
if (Validate::isUnsignedId($this->id_customer)) {
Customer::resetAddressCache($this->id_customer, $this->id);
}
return parent::update($null_values);
}
Instead of changing the PrestaShop core. You can also use the override option.
Create the file yourprojectfolder/override/classes/Address.php and insert this code and save the file:
<?php
/**
* Fix for capitalize and submit the first letters of the name and address input fields
*
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* #author Peter Visser <info#mark-app.com>
*/
class Address extends AddressCore
{
public function update($null_values = false)
{
// Empty related caches
if (isset(self::$_idCountries[$this->id])) {
unset(self::$_idCountries[$this->id]);
}
if (isset(self::$_idZones[$this->id])) {
unset(self::$_idZones[$this->id]);
}
// Capitalize the first name
$this->firstname = ucfirst($this->firstname);
// Capitalize the first name
$this->lastname = ucfirst($this->lastname);
// Capitalize the address fields
$this->address1 = ucfirst($this->address1);
$this->address2 = ucfirst($this->address2);
if (Validate::isUnsignedId($this->id_customer)) {
Customer::resetAddressCache($this->id_customer, $this->id);
}
return parent::update($null_values);
}
public function add($autodate = true, $null_values = false)
{
if (!parent::add($autodate, $null_values)) {
return false;
}
// Capitalize the first name
$this->firstname = ucfirst($this->firstname);
// Capitalize the first name
$this->lastname = ucfirst($this->lastname);
// Capitalize the address fields
$this->address1 = ucfirst($this->address1);
$this->address2 = ucfirst($this->address2);
if (Validate::isUnsignedId($this->id_customer)) {
Customer::resetAddressCache($this->id_customer, $this->id);
}
return true;
}
}
After that reset your cache for overrides bij deleting yourprojectfolder/cache/class_index.php
Source: https://www.prestashop.com/forums/topic/333935-capitalize-first-letters-of-address-input-fields-and-submit-to-database
Lacking the help, I decided to do a little workaround - I've actually set a trigger on the database, which on insert or update transforms the specified data with SQL's UPPER() function.

What does it return in this function return parent::beforeSave();?

I don't know what this function will return, just modified record attributes or anything other?
protected function beforeSave()
{
if ($this->getIsNewRecord())
{
$this->created = Yii::app()->localtime->UTCNow;
}
$this->lastmodified = Yii::app()->localtime->UTCNow;
if ($this->dob == '') {
$this->setAttribute('dob', null);
} else {
$this->dob=date('Y-m-d', strtotime($this->dob));
}
if($this->image!="")
{
$this->imgfile_name = $this->image->name;
$this->imgfile_type = $this->image->type;
$this->imgfile_size = $this->image->size;
}
$this->phone=substr($this->phone,0,3).substr($this->phone,4,3).substr($this->phone,8,4);
return parent::beforeSave();
}
CActiveRecord::beforeSave is supposed to return true if the model is in a valid state and can be saved, and false if it's not.

How to write a custom FindElement routine in Selenium?

I'm trying to figure out how to write a custom FindElement routine in Selenium 2.0 WebDriver. The idea would be something like this:
driver.FindElement(By.Method( (ISearchContext) => {
/* examine search context logic here... */ }));
The anonymous method would examine the ISearchContext and return True if it matches; False otherwise.
I'm digging through the Selenium code, and getting a bit lost. It looks like the actual By.* logic is carried out server-side, not client side. That seems to be complicating matters.
Any suggestions?
I do a multi-staged search. I have a method that performs a try catch and then a method that gets the element. In theory you could do a try catch until instead of this way but I like this way better because of my setup.
public bool CheckUntil(IWebDriver driver, string selectorType, string selectorInfo)
{
int Timer = 160;
bool itemFound = false;
for (int i = 0; i < Timer; i++)
if(itemFound)
{
i = 0
}
else
{
Thread.Sleep(500);
if(selectorType.ToLower() == "id" && TryCatch(driver, selectorType, selectorInfo))
{
if(driver.FindElement(By.Id(selectorInfo).Displayed)
{
itemFound = true;
}
}
else if(selectorType.ToLower() == "tagname" && TryCatch(driver, selectorType, selectorInfo))
{
if(driver.FindElement(By.TagName(selectorInfo).Displayed)
{
itemFound = true;
}
}
}
return itemFound;
}
Here's my try catch method you can add as many different types as you want id, cssselector, xpath, tagname, classname, etc.
public bool TryCatch(IWebDriver driver, string selectorType, string selectorInfo)
{
bool ElementFound = false;
try
{
switch(selectorType)
{
case "id":
driver.FindElement(By.Id(selectorInfo);
break;
case "tagname":
driver.FindElement(By.TagName(selectorInfo);
break;
}
ElementFound = truel
}
catch
{
ElementFound = false;
}
return ElementFound;
}
Ok, I figured out how to do this. I'm leveraging driver.ExecuteScript() to run custom js on the webdriver. It looks a bit like this:
function elementFound(elem) {
var nodeType = navigator.appName == ""Microsoft Internet
Explorer"" ? document.ELEMENT_NODE : Node.ELEMENT_NODE;
if(elem.nodeType == nodeType)
{
/* Element identification logic here */
}
else { return false; }
}
function traverseElement(elem) {
if (elementFound(elem) == true) {
return elem;
}
else {
for (var i = 0; i < elem.childNodes.length; i++) {
var ret = traverseElement(elem.childNodes[i]);
if(ret != null) { return ret; }
}
}
}
return traverseElement(document);