I want to write this raw query using Eloquent. I have tried using ->whereRaw but it shows error due to special characters in it.
SELECT id,shipping_email FROM orders WHERE 1 AND `shipping_email` NOT REGEXP '^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,63}$' AND id = '<>'
Help is appreciated...
Thanks in advance.
I have got answer, I have tried these codes..
\Model\Order::query()
->select('id', 'shipping_email')
->whereRaw("`shipping_email` NOT REGEXP ?", ["^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,63}$"])
->get();
Related
Hello There!
I want to get only the rows where the punch_in_time is greater than the expected_punch_in_time (You can see the columns in the picture link above).
I searched a lot and I did not found anything helpful, I tried the "whereDate" method but it turns out that it only works on datetime fields and not only time as in my case.
Any help would be appreciated!
Thank you.
You can use whereTime function in querybuilder
whereTime('punch_in_time', '>', 'expected_punch_in_time')
for more details refer link https://laravel.com/docs/9.x/queries
Use eloquent: whereTime (colum, '>' colum);
Or if want pure SQL u can use this:
SELECT * FROM testTime WHERE
CAST(punch_in_time AS TIME) > CAST(expected_punch_in_time AS TIME);
SELECT * FROM testTime WHERE
punch_in_time > expected_punch_in_time;
fiddle here
Finally, I figured it out, I have used the "whereColumn" method and it worked well.
public function ScopeLateDrivers($query)
{
return $query->whereColumn('punch_in_time', '>', 'expected_punch_in_time');
}
Here my MySQL query (work in phpMyAdmin) :
SELECT workcenter, (SUM(w1+w2 +w3 +w4)/ (COUNT(DISTINCT(teknisi))*40*4) )*100 AS total FROM `team` GROUP by workcenter ORDER BY total
then, i try in Laravel Sintax like this below (not work) :
$sql = Team::groupBy('workcenter')->select('workcenter', \DB::raw('(SUM(w1+w2+w3+w4)/ (COUNT(DISTINCT(teknisi))*30*4) )*100 AS total'))
->OrderBy('total', 'Desc')
->get();
When i run the laravel sintax, its doesn't show any errors, but the output is nothing..
Please anyone help me to convert the MySQL query to Laravel Sintax. Thank you!
I think you are close enough, however, this doesn't look like a correct way to group by with Eloquent ORM. Try using raw expressions, something like this might work:
$sql = DB::table('team')
->select(DB::raw('workcenter, (SUM(w1+w2 +w3 +w4)/ (COUNT(DISTINCT(teknisi))*40*4) )*100 as total'))
->orderBy('total', 'desc')
->groupBy('workcenter')
->get();
More about raw expressions here - https://laravel.com/docs/6.x/queries#raw-expressions
Whenever I want to convert SQL query to Laravel I always change one column name, the laravel error report will show your current query and u can compare it to the SQL query
How to change this query to Laravel 5.5
"SELECT ROUND((COUNT(STATUS)/(SELECT COUNT(*) FROM data_ap))*100,1)
FROM data_ap WHERE STATUS LIKE '%UP%'";
Something like that :
\DB::table('data_ap')
->select(\DB::raw('ROUND((COUNT(STATUS)/(SELECT COUNT(*) FROM data_ap))*100,1) as calc'))
->where('STATUS','like','%UP%')
->get();
I think you must keep native query with Raw Expressions, for more here https://laravel.com/docs/5.6/queries#raw-expressions
#Muhammad Anwari: You can simply point the targeted column/alias
\DB::table('data_ap')
->select(\DB::raw('ROUND((COUNT(STATUS)/(SELECT COUNT(*) FROM data_ap))*100,1) as calc'))
->where('STATUS','like','%UP%')
->first()->calc;
I have this query in laravel 5.2
$obj_custom_stdy_data = QstCustomStudyData::where('student_id', $this->data_user['student_id'])
->select($list_id . ' as list_id ', 'chapter_id', 'subject_id', 'subject_code_id')
->get()
->toArray();
Well I have a fixed value $list_id got from top code. Actually I want to add new field during query selection as list_id. However I got error for such that method.
When I tried in mysql IDE for example:
SELECT (1+2) as total, c.* FROM users
Then the result is no wrong at all.
Is that anyway to write in query builder for laravel instead of raw style?
You can take the use of DB::raw() method of QueryBuilder like this:
->select(DB::raw('(1+2) as total'));
See more about Query Builder's Raw Expressions
Hope this helps!
I am trying to combine Where And OR in Codeigniter via code.
$this->db->where("id",1);
$this->db->where("status","live")->or_where("status","dead");
Appeared result is as query
where id=1 and status='live' OR status='dead'
Whereas i need result for this query.
where id=1 and (status='live' OR status='dead');
NOTE: Please don't give solution of string passed as parameter from where function. That's not fit into my code. I Just simplified my problem above.
Just found solution, if it can help some one.
$this->db->where("id",1)->where("(status='live' OR status='dead')");
you can use where_in() like this
$status_array = array('live','dead');
$this->db->where("id",1);
$this->db->where_in('status', $status_array);
you just add ->group_start() and ->group_end()
$this->db->where("id",1);
$this->db->where("status","live")
->group_start()
->or_where("status","dead")
->group_end();