Convert raw SQL query to Eloquent - laravel

I need your help, guys. I have this query:
SELECT * FROM users
where lcase(concat(firstname, ' ', lastname)) like lcase(concat('%', replace('%searchterm%', ' ', '%'), '%'))
OR lcase(concat(lastname, ' ', firstname)) like lcase(concat('%', replace('%searchterm%', ' ', '%'), '%'))
OR lcase(location) like lcase('%searchterm%')
Is it possible to use a nice eloquent query instead of DB::select(DB::raw()) to use pagination?

You could use whereRaw and orWhereRaw methods on a Eloquent Builder to use raw expressions while maintaining the ability to get model instances as a result.
The advantages is that you can still use PDO parameter binding and therefore avoid SQL Injections though $searchTerm.
You can also find out more about raw expressions in the documentation;
// Assuming $searchTerm contains the term that you need to search the database for
$users = User::whereRaw('lcase(concat(firstname, " ", lastname)) like lcase(concat("%", replace("%?%", " ", "%"), "%"))', [ $searchTerm ])
->orWhereRaw('lcase(concat(lastname, " ", firstname)) like lcase(concat("%", replace("%?%", " ", "%"), "%"))', [ $searchTerm ])
->orWhereRaw('lcase(location) like lcase("%?%")', [ $searchTerm ])
->get();

To split your where condition you can use orWhere method.
User::
->where(DB::raw('condition_one'))
->orWhere(DB::raw('condition_two'))
->orWhere(DB::raw('condition_three'))
->paginate();

Related

How can I escase string in whereRaw?

In laravel 9 I use whereRaw in code like :
$query->whereRaw( 'item.day ' . $sign . "'".$filter_day."' " );
But I need to escape it.
Function mysql_real_escape_string is absolute now...
What can I use here ? I work with mysql8 now, but I prefer decision for different db on laravel side.
Thanks in advance!
I suggest reading the Laravel documentation thoroughly: https://laravel.com/docs/9.x/queries#raw-methods
You can escape the string using ? as the link I given above.
$query->whereRaw( 'item.day ' . $sign . ' ?', [$filter_day]);
You must NOT escape the operator $sign because its an MYSQL operator.
And also you must make sure that you properly check/sanitize $sign to avoid SQL injection. E.g.
if (in_array($sign, ['=', '<', '>', '<=', '>=', '!=', '<>'])) {
$query->whereRaw( 'item.day ' . $sign . ' ?', [$filter_day]);
}

MYSQL show results that contains hyphen in Laravel?

I have some products with column name that look like product-example. I'm trying to find the same result with case-insensitive if i searched product example or product-example. I have my query but it's not working i have a Syntax error :
$products = Product::whereRaw(
"UPPER('REPLACE(name, '-', ' ')') LIKE '%'".str_replace('-', ' ', $search)."'%'"
)
->get();
MySQL is case-insensitive by default.
As mentioned in the MySQL docs:
SQL pattern matching enables you to use _ to match any single character
so you could replace both " " and "-" with a "_" which would allow you to match either hyphens or spaces:
$products = Product::where('name', 'like', '%' . str_replace([' ', '-'], '_', $search). '%')->get();
Here is a little cheatsheet to help with MySQL wildcards.

Laravel Eloquent Where Query with multiple words

I have the following query (cut for brevity):
$employees = Employee::where('first_name', 'LIKE', "%$query%")
->orWhere('last_name', 'LIKE', "%$query%")
Now this works when the user inputs a single name like 'John' or 'Smith', but when they input 'John Smith' it doesn't find anything. Am I missing an extra orWhere?
Try this :
Employee::where(DB::raw('CONCAT(first_name," ",lastname)'), 'LIKE', "%' . $query . '%"))
You would have to add a 3rd orWhere. For our search function we use something like this:
Employee::whereraw("COALESCE(last_name, '') LIKE '%$query%'")
->Orwhereraw("COALESCE(first_name, '') LIKE '%$query%'")
->Orwhereraw("COALESCE(last_name + ', ' + first_name, '') LIKE '%$query%'")
Adding Coalesce seemed to help with some issues we had when we first implemented it, not sure if it is necessary in your case though.
You can do :
$fullName = trim($query);
$employees = Employee::where(DB::raw("CONCAT(first_name, ' ', last_name)"), 'LIKE', "%".$fullName."%")->get();
You are concatenating the values in database for first_name + ' ' + last_name and then using like to find the matching records.

Joomla database query SELECT AS

So I'm just hypothetically thrilled to be querying my hypothetical database:
$query->select($db->quoteName(array('user_id', 'name')));
I would, however, like the query to look like:
SELECT `user_id` AS `uid`, `name` AS `User-Name`
How the heck do I get the AS in there?
I know this question is 6 months old, so you've probably found an answer or worked around it, but for anyone else who has a similar problem:
$query->select($db->quoteName(array('user_id','name'),array('uid','User-Name')));
If you only want to use an alias for some fields, just pass null in the array for the fields you don't want to alias, so for example:
$query->select($db->quoteName(array('user_id','name'),array(null,'User-Name')));
would give
"SELECT `user_id`, `name` AS `User-Name`"
My preferred way is this:
I create an array with the fields I want to select:
$fields = array(
'a.id' => 'id',
'a.field1' => 'field1',
'a.field2' => 'field2',
'a.field3' => 'field3',
'b.field1' => 'bfield1',
'b.field2' => null,
'b.field3' => 'bfield3',
);
In the above array, the keys are used for the db Column names of the query, the values for the aliases, as you can see later in the $query->select().
*When you do not need an alias - just set null.
This helps better to control and check what fields I want and how to name them - and is better for maintenance or changes and is portable enough, as I only have to change my $fields array according to my needs.
Then the Joomla select command can be like:
$query->select( $db->quoteName(
array_keys($fields),
array_values($fields)
));
This will produce the following SQL SELECT query:
SELECT `a`.`id` AS `id`,`a`.`field1` AS `field1`,`a`.`field2` AS
`field2`,`a`.`field3` AS `field3`,`b`.`field1` AS
`bfield1`, `field2`, `b`.`field3` AS `bfield3`
Try the following:
$query->select($db->quoteName('user_id') .' AS '. $db->quoteName('name') .' AS '. $db->quoteName('User-Name'));
Be sure to use the full query as described here:
http://docs.joomla.org/Selecting_data_using_JDatabase
So yours will look something like this:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('user_id') .' AS '. $db->quoteName('name') .' AS '. $db->quoteName('User-Name'));
$query->from($db->quoteName('#__tablename'));
$db->setQuery($query);
$results = $db->loadObjectList();
Please note that I havent tested this query using AS so let me know if it works or not
I've got this to work:
->select($db->quoteName(array('user_id`' . ' AS ' . '`uid', 'name`' . ' AS ' . '`User-Name')))
It's better to use what Joomla! suggest us.
Note by putting 'a' as a second parameter will generate #__content AS a
So, you may have something like this:
$query
->select('*')
->from($db->quoteName('#__content', 'a'))

How to order by count in Doctrine 2?

I'm trying to group my entity by a field (year) and do a count of it.
Code:
public function countYear()
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('b.year, COUNT(b.id)')
->from('\My\Entity\Album', 'b')
->where('b.year IS NOT NULL')
->addOrderBy('sclr1', 'DESC')
->addGroupBy('b.year');
$query = $qb->getQuery();
die($query->getSQL());
$result = $query->execute();
//die(print_r($result));
return $result;
}
I can't seem to say COUNT(b.id) AS count as it gives an error, and
I do not know what to use as the addOrderby(???, 'DESC') value?
There are many bugs and workarounds required to achieve order by expressions as of v2.3.0 or below:
The order by clause does not support expressions, but you can add a field with the expression to the select and order by it. So it's worth repeating that Tjorriemorrie's own solution actually works:
$qb->select('b.year, COUNT(b.id) AS mycount')
->from('\My\Entity\Album', 'b')
->where('b.year IS NOT NULL')
->orderBy('mycount', 'DESC')
->groupBy('b.year');
Doctrine chokes on equality (e.g. =, LIKE, IS NULL) in the select expression. For those cases the only solution I have found is to use a subselect or self-join:
$qb->select('b, (SELECT count(t.id) FROM \My\Entity\Album AS t '.
'WHERE t.id=b.id AND b.title LIKE :search) AS isTitleMatch')
->from('\My\Entity\Album', 'b')
->where('b.title LIKE :search')
->andWhere('b.description LIKE :search')
->orderBy('isTitleMatch', 'DESC');
To suppress the additional field from the result, you can declare it AS HIDDEN. This way you can use it in the order by without having it in the result.
$qb->select('b.year, COUNT(b.id) AS HIDDEN mycount')
->from('\My\Entity\Album', 'b')
->where('b.year IS NOT NULL')
->orderBy('mycount', 'DESC')
->groupBy('b.year');
what is the error you get when using COUNT(b.id) AS count? it might be because count is a reserved word. try COUNT(b.id) AS idCount, or similar.
alternatively, try $qb->addOrderby('COUNT(b.id)', 'DESC');.
what is your database system (mysql, postgresql, ...)?
If you want your Repository method to return an Entity you cannot use ->select(), but you can use ->addSelect() with a hidden select.
$qb = $this->createQueryBuilder('q')
->addSelect('COUNT(q.id) AS HIDDEN counter')
->orderBy('counter');
$result = $qb->getQuery()->getResult();
$result will be an entity class object.
Please try this code for ci 2 + doctrine 2
$where = " ";
$order_by = " ";
$row = $this->doctrine->em->createQuery("select a from company_group\models\Post a "
.$where." ".$order_by."")
->setMaxResults($data['limit'])
->setFirstResult($data['offset'])
->getResult();`

Resources