laravel query builder without raw? - laravel

i have a query with this code
return $query
->select([
'lineofpro AS pabrik_line',
'date AS tanggal',
DB::raw('ROUND((100*(COUNT( CASE WHEN `tabTravel Card`.qcresu = "Pass" THEN 1 ELSE NULL END ))/count(`tabTravel Card`.lineofpro))) AS lar_percentage'),
DB::raw("ROUND( IFNULL(SUM(`tabTravel Card`.finga),0) / ( count(`tabTravel Card`.lineofpro) * 200) * 1000000) AS finger_a"),
DB::raw("ROUND( IFNULL(SUM(`tabTravel Card`.fingb),0) / ( count(`tabTravel Card`.lineofpro) * 200) * 1000000) AS finger_b"),
DB::raw("ROUND( IFNULL(SUM(`tabTravel Card`.fingc),0) / ( count(`tabTravel Card`.lineofpro) * 200) * 1000000) AS finger_c"),
DB::raw("ROUND( IFNULL(SUM(`tabTravel Card`.fingd),0) / ( count(`tabTravel Card`.lineofpro) * 200) * 1000000) AS finger_d"),
DB::raw("ROUND( IFNULL(SUM(`tabTravel Card`.finge),0) / ( count(`tabTravel Card`.lineofpro) * 200) * 1000000) AS finger_e"),
DB::raw("ROUND( IFNULL(SUM(`tabTravel Card`.crotchf),0) / ( count(`tabTravel Card`.lineofpro) * 200) * 1000000) AS crotch_f"),
DB::raw("ROUND( IFNULL(SUM(`tabTravel Card`.crotchg),0) / ( count(`tabTravel Card`.lineofpro) * 200) * 1000000) AS crotch_g"),
DB::raw("ROUND( IFNULL(SUM(`tabTravel Card`.crotchh),0) / ( count(`tabTravel Card`.lineofpro) * 200) * 1000000) AS crotch_h"),
DB::raw("ROUND( IFNULL(SUM(`tabTravel Card`.crotchi),0) / ( count(`tabTravel Card`.lineofpro) * 200) * 1000000) AS crotch_i"),
DB::raw("ROUND( IFNULL(SUM(`tabTravel Card`.palmj),0) / ( count(`tabTravel Card`.lineofpro) * 200) * 1000000) AS palm_j"),
DB::raw("ROUND( IFNULL(SUM(`tabTravel Card`.cuffk),0) / ( count(`tabTravel Card`.lineofpro) * 200) * 1000000) AS cuff_k"),
DB::raw("ROUND( IFNULL(SUM(`tabTravel Card`.totalpin),0) / ( count(`tabTravel Card`.lineofpro) * 200) * 1000000) AS total_pinhole"),
])
->where([
['date', '>=', $beginDate],
['date', '<=', $endDate],
$this->generatePlanCodeFilter($plan, $line),
])
->groupBy([
'lineofpro',
'date',
]);
is it possible to query this without DB::raw?
i try to create dynamic query
but using an array to return the select value with DB::raw will only return '' string

You can define your fields as accessors and move your calculation logic there.
Then you can build the query using Eloquent.
Ex:
public function getTotalPinholeAttribute()
{
// calculation logic here
}
You can read more about accessors here: https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor

Related

case statement in Dax

I have the following case when statement:
case when ts.wgt_kg / ((hgt_cm / 100) * (hgt_cm / 100)) < 18.5 then 'Underweight < 18.5'
when ts.wgt_kg / ((hgt_cm / 100) * (hgt_cm / 100)) between 18.5 and 24.9 then 'Normal 18.5-24.9'
when ts.wgt_kg / ((hgt_cm / 100) * (hgt_cm / 100)) between 25.0 and 29.9 then 'Overweight 25-29.9'
when ts.wgt_kg / ((hgt_cm / 100) * (hgt_cm / 100)) > 30.0 then 'Obese > 30.0'
end as BMI
How can i convert it into DAX? I tried to google it but I wasn't able to find anything useful. Can someone help me with that please.
Thanks
Try something along these lines:
BMI Category =
VAR BMI = ts.wgt_kg / ( ( hgt_cm / 100 ) * ( hgt_cm / 100 ) )
RETURN
SWITCH (
TRUE (),
BMI < 18.5, "Underweight < 18.5",
BMI < 25.0, "Normal 18.5-24.9",
BMI < 30.0, "Overweight 25-29.9",
"Obese > 30.0"
)
This will return the first condition that evaluates to true or use the last argument if none of the above are true.

How to calculate latitude and longitude based on distance in laravel

This is my function
public function getRshow(){
$lats = $tenl[0]->latitude;
$longs = $tlog[0]->longitude;
$distance =$par_dis[0]->partner_distance;
$project = "SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(( $lats - latitude) * pi()/180 / 2), 2) +COS( $lats * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( $longs - longitude) * pi()/180 / 2), 2) ))) as distance
FROM `abserve_renterpost` having distance <= $distance order by distance";
$la=DB::SELECT($project);
$lara=array();
foreach ($project as $key => $v) {
$lara[]=(get_object_vars($v));
}
}
My query is working good if I run only the query, but if I converted my query for value and array format it's not working, error is displayed as below.
(SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' - latitude) * pi()/180 / 2), 2) +COS( 27.7 * pi()/180) * COS(latitude * ' at line 1 (SQL: SELECT * , (3956 * 2 * ASIN(SQRT( POWER(SIN(( 27.7 - latitude) * pi()/180 / 2), 2) +COS( 27.7 * pi()/180) * COS(latitude * pi()/180) * POWER(SIN(( 74.12399600000003
- longitude) * pi()/180 / 2), 2) ))) as distance
FROM `abserve_renterpost` having distance <= 200 order by distance))
I noticed $lats was 27.7 which is odd.
try this
$lats = $tenl[0]->latitude;
$longs = $tlog[0]->longitude;
$distance =$par_dis[0]->partner_distance;
$project = <<<SQL
SELECT * , (3956 * 2 * ASIN(
SQRT(
POWER(SIN(($lats - `latitude`) * pi() / 180 / 2), 2)
+ COS($lats * pi() / 180)
* COS(`latitude` * pi() / 180)
* POWER(SIN(($longs - `longitude`) * pi() / 180 / 2), 2)
))
) as 'distance'
FROM `abserve_renterpost`
WHERE distance <= $distance
ORDER BY distance;
SQL;
$la=DB::select($project);
$lara=array();
foreach ($project as $key => $v) {
$lara[]=(get_object_vars($v));
}

Getting compilation errors with this function

I am getting the following error:
Warning: compiled but with compilation errors
when I try to create the following function:
Create or replace function Time_Gap (v_date1, v_date2, return varchar2 )
is
difrence_In_Hours varchar2;
difrence_In_minutes varchar2;
difrence_In_seconds varchar2;
begin
difrence_In_Hours := floor(((v_date2-v_date1)*24*60*60)/3600);
difrence_In_minutes := floor((((v_date2-v_date1)*24*60*60) -
floor(((v_date2-v_date1)*24*60*60)/3600)*3600)/60);
difrence_In_seconds := round((((v_date2-v_date1)*24*60*60) -
floor(((v_date2-v_date1)*24*60*60)/3600)*3600 -
(floor((((v_date2-v_date1)*24*60*60) -
floor(((v_date2-v_date1)*24*60*60)/3600)*3600)/60)*60) ));
return difrence_In_Hours || '' HRS '' || difrence_In_minutes || '' MINS '' || difrence_In_seconds
|| '' SECS '';
end ;
/
what am I doing wrong???
Thanks,
a few syntax errors, return type should be outside of input parameters and define type of input:
create or replace function time_gap (v_date1 in date, v_date2 in date)
return varchar2 is
difrence_in_hours varchar2(500);
difrence_in_minutes varchar2(500);
difrence_in_seconds varchar2(500);
begin
difrence_in_hours := floor ( ( (v_date2 - v_date1) * 24 * 60 * 60) / 3600);
difrence_in_minutes :=
floor ( ( ( (v_date2 - v_date1) * 24 * 60 * 60) - floor ( ( (v_date2 - v_date1) * 24 * 60 * 60) / 3600) * 3600) / 60);
difrence_in_seconds :=
round (
( ( (v_date2 - v_date1) * 24 * 60 * 60)
- floor ( ( (v_date2 - v_date1) * 24 * 60 * 60) / 3600) * 3600
- ( floor (
( ( (v_date2 - v_date1) * 24 * 60 * 60) - floor ( ( (v_date2 - v_date1) * 24 * 60 * 60) / 3600) * 3600)
/ 60)
* 60)));
return difrence_in_hours || ' HRS ' || difrence_in_minutes || ' MINS ' || difrence_in_seconds || ' SECS';
end;
/
EDIT:
You have to give us more of the error message. It's not saying anything. This works fine for me:
declare
function time_gap (v_date1 in date, v_date2 in date)
return varchar2 is
difrence_in_hours number;
difrence_in_minutes number;
difrence_in_seconds number;
begin
difrence_in_hours := floor ( ( (v_date2 - v_date1) * 24 * 60 * 60) / 3600);
difrence_in_minutes :=
floor ( ( ( (v_date2 - v_date1) * 24 * 60 * 60) - floor ( ( (v_date2 - v_date1) * 24 * 60 * 60) / 3600) * 3600) / 60);
difrence_in_seconds :=
round (
( ( (v_date2 - v_date1) * 24 * 60 * 60)
- floor ( ( (v_date2 - v_date1) * 24 * 60 * 60) / 3600) * 3600
- ( floor (
( ( (v_date2 - v_date1) * 24 * 60 * 60)
- floor ( ( (v_date2 - v_date1) * 24 * 60 * 60) / 3600) * 3600)
/ 60)
* 60)));
return trim (to_char (difrence_in_hours)) || ' HRS ' || trim (to_char (difrence_in_minutes)) || ' MINS ' || trim (
to_char (
difrence_in_seconds)) || ' SECS';
end;
begin
dbms_output.put_line (time_gap (sysdate, sysdate - 2));
end;
==>
PL/SQL block executed
-48 HRS 0 MINS 0 SECS
Have you created it without checking the syntax? Always check the syntax before doing stuff like that.
As per https://docs.oracle.com/cd/B12037_01/server.101/b10759/create_function.gif just in the first line:
you lack one of [IN][OUT][IN OUT] at every argument
you lack datatypes for every argument
return statement should be outside the parentheses.
At that point I stopped reading.

yii query not caching when using createCommand

I have the following query being logged in mysql which is not being cached.
SELECT
*,
round(priceperkm * IFNULL(KMPlanned, projects.KM),
4) AS NEWVALUE,
round(PRICE * IFNULL(KMPlanned, projects.KM),
2) AS TotalPriceForItem,
SUM(((abs(FSP - LSP) + 1) * SI) / 1000) AS KM_Completed,
round(SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE,
2) AS TotalPrice,
if(round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, projects.KM)) * 100,
2)>100,100,round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, projects.KM)) * 100,
2)) AS TotalPercent
FROM
hdb.projects
join
biditems ON projects.id = biditems.project_id
join
lookupprocess ON biditems.ITEMID = lookupprocess.biditems_id
left join
jobsprocesscomplete ON lookupprocess.ID = lookupprocess_id
left join
detailsseismic ON jobsprocesscomplete.JOBNO = detailsseismic.JOBNO
where
projects.PROJID = '1407075'
and lookupprocess.ID = 16299
The Yii code is as follows.
$dependancy = new CDbCacheDependency("select last_modified_date from projects where PROJID = $projid");
$countJobs = Jobs::model()->cache(CACHE_TIMEOUT,$dependancy)->count(array("condition"=>"PROJID=$projid"));
foreach ($processstages as $k=>$v) {
$sql = "SELECT
*,
round(priceperkm * IFNULL(KMPlanned, projects.KM),
4) AS NEWVALUE,
round(PRICE * IFNULL(KMPlanned, projects.KM),
2) AS TotalPriceForItem,
SUM(((abs(FSP - LSP) + 1) * SI) / 1000) AS KM_Completed,
round(SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE,
2) AS TotalPrice,
if(round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, projects.KM)) * 100,
2)>100,100,round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, projects.KM)) * 100,
2)) AS TotalPercent
FROM
hdb.projects
join
biditems ON projects.id = biditems.project_id
join
lookupprocess ON biditems.ITEMID = lookupprocess.biditems_id
left join
jobsprocesscomplete ON lookupprocess.ID = lookupprocess_id
left join
details".$type['type']." ON jobsprocesscomplete.JOBNO = details".$type['type'].".JOBNO
where
projects.PROJID = :pid
and lookupprocess.ID = :lid
";
$command = Yii::app()->db->cache(CACHE_TIMEOUT,$dependancy)->createCommand($sql); // need to set memecahce to 50m to cache this query
//Yii::log("select last_modified_date from projects where PROJID = $projid",CLogger::LEVEL_INFO, __METHOD__);
$command->bindValue(":lid",$k,PDO::PARAM_INT);
$command->bindValue(":pid",$projid,PDO::PARAM_INT);
Yii::log((memory_get_peak_usage(true))/1024/1024 . "MB",CLogger::LEVEL_INFO, __METHOD__);
$query = $command->query();
}
I cannot seem to figure out why it is not caching these results as the last_modified_date is not changing. i have set max item cache to 32m and memcache memory to 512.
I have rewrote my query to as below which gets cached but the createCommand way it does not
$criteria=new CDbCriteria;
$criteria->select = '
round(priceperkm * IFNULL(KMPlanned, t.KM),
4) AS NEWVALUE,
round(PRICE * IFNULL(KMPlanned, t.KM),
2) AS TotalPriceForItem,
SUM(((abs(FSP - LSP) + 1) * SI) / 1000) AS KM_Completed,
round(SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE,
2) AS TotalPrice,
if(round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, t.KM)) * 100,
2)>100,100,round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, t.KM)) * 100,
2)) AS TotalPercent';
$criteria->join = 'join biditems ON t.id = biditems.project_id ';
$criteria->join .= 'join lookupprocess ON biditems.ITEMID = lookupprocess.biditems_id ';
$criteria->join .= 'left join jobsprocesscomplete ON lookupprocess.ID = lookupprocess_id ';
$criteria->join .= 'left join details'.$type['type'].' ON jobsprocesscomplete.JOBNO = details'.$type['type'].'.JOBNO ';
$criteria->compare('t.PROJID',$projid);
$criteria->compare('lookupprocess.ID',$k);
Projects::model()->cache(CACHE_TIMEOUT,$dependancy)->findAll($criteria);
Try to put the following:
$result = Yii::app()->db->cache(self::CACHE_TIMEOUT,$dependancy)
->createCommand($sql)
->bindValue(":lid",$k,PDO::PARAM_INT);
->bindValue(":pid",$projid,PDO::PARAM_INT)
->findAll();
I assumed that CACHE_TIMEOUT was a constant in this class, but if it is defined in another class, then you need to adapt this part

Calculating Brightness and Contrast similar to new Adobe Photoshop or Lightroom functions

I am trying to create Brightness and Contrast filters similar to new Adobe Photoshop or Lightroom Brightness and Contrast filters. I am doing RGB -> XYZ - xyY conversion and with increasing pixel Brightness (Y) ( not linear increase, Brightness increase is calculated depending on pixels Brightness) and then converting back to XYZ and RGB trying to get as similar as possible values to test image that I used to increase Brightness in Adobe. I am not getting similar results as I think I am missing something, maybe gamma calculation or as images are in sRGB space some color space conversion, or I simple have bug in Color conversion code? So here are Color conversion function I used (in Objective-C) :
-(void) convertRGBcolor:(cColorRGB*) pRGB toXYZcolor:(zColorXYZ*) pXYZ
{
if(pRGB == nil || pXYZ == nil) {
return;
}
double var_R = ( pRGB->r / 255.0 );
double var_G = ( pRGB->g / 255.0 );
double var_B = ( pRGB->b / 255.0 );
if ( var_R > 0.04045 ) var_R = pow((( var_R + 0.055 ) / 1.055 ) , 2.4);
else var_R = var_R / 12.92;
if ( var_G > 0.04045 ) var_G = pow((( var_G + 0.055 ) / 1.055 ) , 2.4);
else var_G = var_G / 12.92;
if ( var_B > 0.04045 ) var_B = pow((( var_B + 0.055 ) / 1.055 ) , 2.4);
else var_B = var_B / 12.92;
var_R = var_R * 100;
var_G = var_G * 100;
var_B = var_B * 100;
//Observer. = 2°, Illuminant = D65
pXYZ->X = var_R * 0.4124 + var_G * 0.3576 + var_B * 0.1805;
pXYZ->Y = var_R * 0.2126 + var_G * 0.7152 + var_B * 0.0722;
pXYZ->Z = var_R * 0.0193 + var_G * 0.1192 + var_B * 0.9505;
}
-(void) convertXYZcolor:(zColorXYZ*) pXYZ toRGBcolor:(cColorRGB*) pRGB
{
if(pRGB == nil || pXYZ == nil) {
return;
}
double var_X = pXYZ->X / 100; //X from 0 to 95.047
double var_Y = pXYZ->Y / 100; //Y from 0 to 100.000
double var_Z = pXYZ->Z / 100; //Z from 0 to 108.883
double var_R = var_X * 3.2406 + var_Y * -1.5372 + var_Z * -0.4986;
double var_G = var_X * -0.9689 + var_Y * 1.8758 + var_Z * 0.0415;
double var_B = var_X * 0.0557 + var_Y * -0.2040 + var_Z * 1.0570;
if ( var_R > 0.0031308 ) var_R = 1.055 * ( pow( var_R,(1/2.4))) - 0.055;
else var_R = 12.92 * var_R;
if ( var_G > 0.0031308 ) var_G = 1.055 * ( pow( var_G,(1/2.4))) - 0.055;
else var_G = 12.92 * var_G;
if ( var_B > 0.0031308 ) var_B = 1.055 * ( pow( var_B,(1/2.4))) - 0.055;
else var_B = 12.92 * var_B;
pRGB->r = var_R * 255;
pRGB->g = var_G * 255;
pRGB->b = var_B * 255;
}
-(void) convertYxycolor:(zColorYxy*) pYxy toXYZcolor:(zColorXYZ*) pXYZ
{
if(pYxy == nil || pXYZ == nil) {
return;
}
pXYZ->X = pYxy->x * ( pYxy->Y / pYxy->y );
pXYZ->Y = pYxy->Y;
pXYZ->Z = ( 1 - pYxy->x - pYxy->y ) * ( pYxy->Y / pYxy->y );
}
-(void) convertXYZcolor:(zColorXYZ*) pXYZ toYxycolor:(zColorYxy*) pYxy
{
if(pYxy == nil || pXYZ == nil) {
return;
}
pYxy->Y = pXYZ->Y;
pYxy->x = pXYZ->X / ( pXYZ->X + pXYZ->Y + pXYZ->Z );
pYxy->y = pXYZ->Y / ( pXYZ->X + pXYZ->Y + pXYZ->Z );
}

Resources