WhereRaw Laravel with variable - laravel

I'm trying to do this query but I got an error (Malformed UTF-8 characters, possibly incorrectly encoded):
DB::table('my_table')->select(DB::raw("id"))
->whereRaw('UPPER(name)','=', $upper_name)
->pluck('id')->first();
I'm trying to add the UPPER sql function to the query. With direct sql, the query should be:
select * from my_table
where UPPER(name) = 'HELLO'
Where $upper_name = HELLO.

Simpliest Way to do that. Hope will help you
DB::table('my_table')->select('id')
->where(DB::raw("UCASE(name)"), $upper_name)
->first();
UCASE - Convert the text to upper-case

Bah... I got it:
DB::table('my_table')->select(DB::raw("id"))
->whereRaw('UPPER(name) = ?', $upper_name)
->pluck('id')->first();

Related

codeigniter use concat regex in where clause

I have a custom query like this
SELECT `wo_number`,`request_date`,`wo_type`,`requestor`
FROM `work_orders`
WHERE CONCAT(",", `assigned_to_enggs`, ",") REGEXP ",(21),"
It works fine for me I want to use it in Codeigniter Active Record. I have tried something like
$this->db->select('wo_number,request_date,wo_type,requestor')
->from('work_orders')
->where("CONCAT(',', assigned_to_enggs, ',') REGEXP ',(21),'");
Don't know what I am doing wrong in syntax. can someone guide me. Thanks
Please try below case in where and use result() for get result array
$this->db->select('wo_number,request_date,wo_type,requestor');
$this->db->where("CONCAT(',', assigned_to_enggs, ',') REGEXP ',(21),'", NULL, FALSE);
$this->db->select->from('work_orders');
$query = $this->db->get()->result();
If not work than you also write query like below in codeigniter.
$query = $this->db->query('YOUR QUERY HERE');
$query->result()

laravel return '?' instead of '1' in WHERE clause

$datas=Elan::orderBy('created_at','desc')->where('status', 1)->take(4)->toSql();
dd($datas);
I wrote query in "laravel 5.2". I displayed it as sql query with function toSql()
and the result is:
"select * from `els` where `status` = ? order by `created_at` desc limit 4"
you can see that there is ? mark. this is why my query doesn't work. why does it return me ? instead of one. I also tried it like this '1' same result.
You may want to read about bindings if you want to understand what does ? mean.
https://laravel.com/docs/5.3/database#running-queries
If you want to debug and watch queries, you can install this debugbar: https://github.com/barryvdh/laravel-debugbar
? is used for Parameter binding which prevent against SQL injection.
If you want to see the raw SQL query which is executed with parameters values, try the below code:
DB::enableQueryLog();
$datas=Elan::orderBy('created_at','desc')->where('status', 1)->take(4)->get();
dd(DB::getQueryLog());
Hope this helps!

Laravel get all the values of a column beginning with a number

I have a model called "Image" and a table called "images". The table has a column for the "id" and another for the "name". I need to fetch only the rows with the name beginning with a number.
I need to fetch are called something like
16783521_facebook.png
While the others are something like...
twiter.png
Try this:
Image::whereRaw("name REGEXP '^[0-9]'") -> get();
If it's something you're going to use in more than 1 place, consider moving it to a scope.
In your image model define something like:
public function scopeNumeric($query)
{
return $query -> whereRaw("name REGEXP '^[0-9]'");
}
Then you can just use:
Image::numeric() -> get();
I dont know much about laravel, but this plain query will help -
SELECT * FROM mytable WHERE mycolumn REGEXP '^[0-9]+$' or
SELECT * FROM myTable WHERE col1 REGEXP '[0-9]+';
Laravel doesn't have that built-in, so you'll have to make do with raw queries. In its base form:
$results = SomeModel::whereRaw("some_column REGEXP '^[0-9]'")->get();
You can modify this as usual with selects, other limitations, etc. as you require.
Filter the images after the query using one of the collection methods. Like below solved me.
$onlyNumeric = $photos->filter(function ($value, $key) {
return is_numeric(substr($value, 0, 1));
});

Laravel Query Builder: whereExists translates condition clause to question mark

If I have the following query built using the Query Builder:
$q = DB::table('Products')->whereExist(function ($q)
{
$q->select(DB::raw(1))
->from('tags_products')
->where('products.PorductId', '=', 'tags_products.ProductID');
});
The translated SQL using $q->toSql(); that is:
select * from `Products` where `exist` = (select 1 from `tags_products` where `products`.`ProductID` = ?)
Apparently, the Query Builder translates tags_products.ProductID to ?.
Why does it become "?" ?
As #Jared Eitnier very well pointed out, Laravel uses PDO to bind the parameters you pass to the Query Builder methods. However, because when you use where the third parameter represents the value, Laravel will not treat it as a column unless you explicitly tell it to, otherwise it will treat 'tags_products.ProductID' as a regular string value. So you have two options here:
1. Use DB::raw() to let the Query Builder know that the value is not a string that requires escaping:
->where('products.PorductId', '=', DB::raw('tags_products.ProductID'));
2. Use whereRaw() which will allow you to write a raw SQL statement:
->whereRaw('products.PorductId = tags_products.ProductID');
These are mysql prepared statements.
See What is the question mark's significance in MySQL at "WHERE column = ?"?
Laravel uses mysql's PDO.

Doctrine query not executing

trying to execute a Doctrine query that is invariably not happening:
The code I have:
$q = Doctrine_Query::Create()->select('l.lid')->from('lessons l')->where('l.topic =?','Title of topic')
$result = $q->fetchOne() ;
The funny thing is, this returns the wrong column, that is not l.lid but l.someOtherColumn
So not sure where I am goofing up, your comments and critics are much appreciated.
Thanks!
Try to use Create('table_name t ') instead of ->from() ....

Resources