codeigniter use concat regex in where clause - codeigniter

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()

Related

WhereRaw Laravel with variable

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();

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));
});

Doctrine Where 'IN' operator

This portion of a Doctrine query is only returning results for "validated = 1". How do I modify this query so it will also include results for validated = 3? This is my first "IN".
$query
->andWhere($query->expr()->in('m.validated', ':validated'))
->setParameter('validated', '1,3');
Try this code instead,
$query
->andWhere('m.validated IN (:validated)')
->setParameter('validated', array('1','2'));
Or with the same code giving values in an array.
Hope this helps.
Cheers!

Magento collection expression

How to use expressions in a collection.
More specific, I want to SELECT CAST(qty_shipped AS UNSIGNED).
I have tried something like this:
addExpressionFieldToSelect('qty_shipped','CAST(qty_shipped AS UNSIGNED)',null)
, but it appends the table name for some reason.
Thanks.
Try as:
$collection->getSelect()->columns(array('qty_shipped' => new Zend_Db_Expr ('CAST(qty_shipped AS UNSIGNED)')));
Where $collection = Collection Object
Let me know if that works for you.

Resources