Case When Like in Eloquent in Laravel - laravel

i'm new with eloquent laravel
curently i have working mysql query below to transform data
select * from `dsa_data_realisasi_resolver` where (CASE WHEN akun LIKE '%51%' THEN 51 ELSE 0 END ) and year(`tanggal`) = 2020 and `kdsatker` = 412772 group by `akun`
when i try to use eloquent with something like this
$realisasi=Realisasi::whereRaw('(CASE WHEN akun LIKE '%51%' THEN 51 ELSE 0 END )')
->whereYear('tanggal','2020')
->where('kdsatker','412772')
->groupby('akun')
->get();
dd($realisasi);
there are errors
ErrorException
A non-numeric value encountered
when i try to remove single quotes '
$realisasi=Realisasi::whereRaw('(CASE WHEN akun LIKE %51% THEN 51 ELSE 0 END )')
->whereYear('tanggal','2020')
->where('kdsatker','412772')
->groupby('akun')
->get();
dd($realisasi);
the errors are
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%51% THEN 51 ELSE 0 END ) and year(`tanggal`) = ? and `kdsatker` = ? group by `a' at line 1 (SQL: select * from `dsa_data_realisasi_resolver` where (CASE WHEN akun LIKE %51% THEN 51 ELSE 0 END ) and year(`tanggal`) = 2020 and `kdsatker` = 412772 group by `akun`)
is there any solution with eloquent for query with case when like '%data%'
Thanks

Try with "(CASE WHEN akun LIKE '%51%' THEN 51 ELSE 0 END )" Notice the double quote to contain the whereRaw statement.
And to send dynamic data you can try "(CASE WHEN akun LIKE '%".data."%' THEN 51 ELSE 0 END )"
So your query should look like this:
$realisasi=Realisasi::whereRaw("(CASE WHEN akun LIKE '%".$data."%' THEN 51 ELSE 0 END )')
->whereYear('tanggal','2020')
->where('kdsatker','412772')
->groupby('akun')
->get();
MySQL requires the like pattern to be inside single quotes. For more reference: https://dev.mysql.com/doc/refman/8.0/en/pattern-matching.html

Related

How to use date condition in Symfony (doctrine + oracle)?

How to use date condition in Symfony (doctrine + oracle) ?
I have trying
$qb = $this->createQueryBuilder('la')
->select('la.id')
->andWhere("la.dateCreated >= TO_DATE(':a', 'yyyy-mm-dd')")
->setParameter('a', '2020-09-01')
->getQuery();
However is error: the query defines 0 parameters and you bound 1
Any Ideas?
In your code, I don't think there should be quotes on ':a', I'd put :
TO_DATE(:a, 'yyyy-mm-dd')
Beside, how about :
$qb = $this->createQueryBuilder('la')
->select('la.id')
->andWhere("la.dateCreated >= :a)
->setParameter('a', '2020-09-01')
->getQuery();

How to combined duplicate row to one row in laravel 5.7 controller

I want to combined the duplicate row to one row and show the result in front end but i face some error please any one help .
time_session | day
------------ ----
1 saturday
2 friday
2 friday
3 wednessday
this is my controller code:
public function classSchedule()
{
$duplicates = DB::table('applications')
->join('batches','applications.time_session' ,'=','batches.id')
->select( 'applications.*' , 'batches.*')
->groupBy('applications.time_session')
->get();
return view('backEnd.classes.manageClass1',['duplicates'=>$duplicates]);}
this is my front end code:
<td> {{$duplicate->time_session }} </td>
time_session | day taking day
------------ ---- ----------
1 saturday 1
2 friday 2
3 wednessday 1
SQLSTATE[42000]: Syntax error or access violation: 1055 'pixelmetro.applications.id' isn't in GROUP BY (SQL: select `applications`.*, `batches`.* from `applications` inner join `batches` on `applications`.`time_session` = `batches`.`id` group by `applications`.`time_session`)
Actual out put :
https://www.facebook.com/photo.php?fbid=2317443465016247&set=pcb.442972496253366&type=3&theater&ifg=1
This 1055 error is a MySQL issue that can be resolved changing Laravel config. Go to config/database.php file and change mysql property strict from true to false. 'strict' => false,
If you don't want to change it, then you can add a new property modes
'modes' => [
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_AUTO_CREATE_USER',
'NO_ENGINE_SUBSTITUTION'
]
And yeah don't forget to run the command php artisan config:cache after making changes.
To get desired result you need to use count.
$duplicates = DB::table('applications')
->join('batches','applications.time_session' ,'=','batches.id')
->select( 'applications.*' , 'batches.*', DB::raw('COUNT(applications.time_session) as counter')
->groupBy('applications.time_session')
->get();
In blade:
#foreach($duplicates as $duplicate)
<td>$duplicate->counter</td>
#endforeach
i think you have some miss conception on group by.if you group by with a certain column then you have to get others column with aggregate function.
in your case you made group by with (time_session) and you try to get all from the table that's why you got the error.
solution: you can make group by with both (time_session and day) then your problem will be solve.
sample code:
public function classSchedule()
{
$duplicates = DB::table('applications')
->join('batches','applications.time_session' ,'=','batches.id')
->select( 'applications.*' , 'batches.*')
->groupBy('applications.time_session')
->groupBy('applications.day')
->get();
return view('backEnd.classes.manageClass1',['duplicates'=>$duplicates]);}

SQL Syntax Error 1064 (MyBB)

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY price ASC' at line 1
Query:
SELECT * FROM mybb_newpoints_subscriptions WHERE sid IN() ORDER BY price ASC
Code snip:
$subplans = '';
if($sids != '')
{
$where = 'sid IN('.implode(',', $sids).')';
$query = $db->simple_select('newpoints_subscriptions', '*', $where, array('order_by' => 'price', 'order_dir' => 'ASC'));
while ($sub = $db->fetch_array($query))
How can I fi​​​​x th​​​is? It's been a​​​ problem for a while and ​​​​​​​​I am not very experienced with P​​HP. If you need more d​​​​eetz let me k​​​​​now. Th​​​anks! :)​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

I Have 2 1064 SQL Errors And Can Not Figure Them Out

This code worked good on ph 5.3. My server got upgraded to php5.6 and now the code no longer works.
Here is the two statements.
$sql = "UPDATE " . FORUM_TOUR_TABLE . "
SET page_subject = '$subject', page_text = '$message', page_access = $page_access
WHERE page_id = $id";
It says :
`SQL Error : 1064 Syntax error near 'WHERE page_id = 1' at line 3
UPDATE SET phpbb_forum_tour page_subject = ' Welcome To The Tower' , page_text = ' Update me please ' = WHERE page_access page_id = 1
Line : 172
File: admin_forum_tour.php`
That happens when I try to update a post.
When I try to add a new post using this code:
$sql = "INSERT INTO " . FORUM_TOUR_TABLE . " (page_id, page_subject, page_text, page_sort, bbcode_uid, page_access)
VALUES ($id, '$subject', '$message', $page, '$bbcode_uid', $page_access)";
This gives me this:
`SQL Error : 1064 Syntax error near ')' at line 2
INSERT INTO phpbb_forum_tour ( page_id , page_subject , page_text , page_sort , bbcode_uid , page_access ) VALUES (3, '2 Word Rule ', ' Hello World I am here ', 30 , ' f4e1be18dc ' )
Line : 198
File: admin_forum_tour.php`
I 100% apologize for asking this as I am more than sure it has been beat to death. I have been searching for a couple hours now and have all but given up. I had it working once but was told I opened my self up to injection. I am not a pro coder. I get lucky sometimes and make things work. I am sure these errors and code is basic to most but I am still learning.
Thanks for helping and understanding my "newbieness".

Problem in oracle query

Hai guys,
I've a query in which i need to interchange the values of two fields.
The query is as follows:
SELECT TO_DATE(A.G_LEDGER_DATE,'dd/mm/YYY')as G_LEDGER_DATE,C.ACC_MASTER_NAME,
A.G_LEDGER_REF_NO ,
NVL(CASE WHEN B.G_LEDGER_SECTION = 1 THEN
CASE WHEN
(SELECT COUNT(*)FROM SOSTRANS.ACC_GEN_LEDGER WHERE G_LEDGER_SECTION = B.G_LEDGER_SECTION AND G_LEDGER_ID = B.G_LEDGER_ID)> 1 THEN
B.G_LEDGER_VALUE ELSE A.G_LEDGER_VALUE END END,0) AS G_LEDGER_DR_VALUE,
NVL(CASE WHEN B.G_LEDGER_SECTION = -1 THEN
CASE WHEN
(SELECT COUNT(*) FROM SOSTRANS.ACC_GEN_LEDGER WHERE G_LEDGER_SECTION = B.G_LEDGER_SECTION AND G_LEDGER_ID = B.G_LEDGER_ID)> 1 THEN
B.G_LEDGER_VALUE ELSE A.G_LEDGER_VALUE END END,0) AS G_LEDGER_CR_VALUE,
B.G_LEDGER_SECTION,C.ACC_MASTER_ID,SUBSTR(A.G_LEDGER_REF_NO,0,3) AS Types,'Z' as OrderChar ,
CASE WHEN A.G_LEDGER_REMARK IS NULL THEN B.G_LEDGER_REMARK ELSE A.G_LEDGER_REMARK END AS Narration
FROM SOSTRANS.ACC_GEN_LEDGER A
LEFT OUTER JOIN SOSTRANS.ACC_GEN_LEDGER B ON A.G_LEDGER_ID = B.G_LEDGER_ID
LEFT OUTER JOIN SOSMASTER.ACC_ACCOUNT_MASTER C ON A.ACC_MASTER_ID = C.ACC_MASTER_ID WHERE A.G_LEDGER_CANCEL='N' AND
B.ACC_MASTER_ID = 'MSOS000001' AND
A.ACC_MASTER_ID <> 'MSOS000001' AND
A.G_LEDGER_SECTION <> B.G_LEDGER_SECTION AND
A.G_LEDGER_DATE >= '25/sep/2009' AND
A.G_LEDGER_DATE<='26/sep/2009'
ORDER BY OrderChar,G_LEDGER_DATE
Now i get the output as
... G_LEDGER_DR_VALUE G_LEDGER_CR_VALUE .....
... 2000 0 .....
... 3000 0 .....
... -1000 0 .....
I need to get the negetive value of the G_LEDGER_DR_VALUE side in G_LEDGER_CR_VALUE and if negetive value exists in G_LEDGER_CR_VALUE then it should be in the G_LEDGER_DR_VALUE field
Can anyone help me to solve this?
If I understood your question well, you select a value (that I will call g_ledger_value) that you want to appear in a different column depending on its sign.
This is how I would do it :
SELECT
CASE WHEN t.g_ledger_value>0 THEN t.g_ledger_value ELSE 0 END AS g_ledger_dr_value,
CASE WHEN t.g_ledger_value<0 THEN t.g_ledger_value ELSE 0 END AS g_ledger_cr_value
FROM
(SELECT g_ledger_value FROM mytable) t;
It sounds like a combination of SIGN() and CASE is what you need ...
CASE WHEN SIGN(G_LEDGER_DR_VALUE) = -1 then ...
ELSE ...
END
etc
SELECT G_LEDGER_DR_VALUE,
CASE WHEN G_LEDGER_DR_VALUE < 0
THEN G_LEDGER_CR_VALUE
ELSE G_LEDGER_DR_VALUE
END
FROM (...)
Is it that you mean? I suggest calculate values of CR___VALUE and DR_VALUE in subquery, and then in wrapping query make CASE which returns you correct value.

Resources