How to convert to laravel eloquent - laravel

I need to convert this query to laravel, Help.
UPDATE table_name
SET usercontact_status = CASE
WHEN usercontact_status = 0 THEN 2
WHEN usercontact_status = 16 THEN 64
WHEN usercontact_status = 32 THEN 128
WHEN usercontact_status = 4 THEN 256
END WHERE usercontact_value = "456"

Here is the edited answer for you question
switch ($usercontact_status) {
case 0:
$value = 2;
break;
case 16:
$value = 64;
break;
case 32:
$value = 128;
break;
case 4:
$value = 256;
break;
}
Your query will be somewhat like this
$query = Model::where('usercontact_value','456')->where('usercontact_status',$usercontact_status)->update([
'usercontact_status' => $value
]);
Hope this solves your problem

Related

How can I use an invalid object in an if statement?

I am currently working on an automatisation script for Indesign certificats.
I am sorting the pages according to the Year and the certNumber.
I am using absolute postions (I did not manage to find an easier Way, but I am sure there is one).
My problem is, that I have 2 variants of each template, that have different absolute psotions of the terms that I use as Keys for sorting, therefor I wrote a "somewhat-swap-funtion", but keep getting errors, because if the next Page, has a different template, i am using an object, which is invalid. How can I work around this problem? I tried the isValid function, but there really isnt that much of documentation online, so I might have used it wrong. Thank you in advance!!
var sortPosArr = [[5,6,4,6],[9,8,6,8],[6,6,5,6],[4,6,4,6],[2,5],[3,7],[4,5],[2,5],[3,5],[6,7],[6,5,6,5],[7,5,7,5],[6,6,6,6],[5,6,5,6],[7,6,7,6],[7,6,7,6]];
var sCN; //sortCaseNumber
var numberOfPages = document.pages.length;
if (auswahlMz != "VERs" && auswahlMz != "HKN NEU100 mit CO2-Einsparung") { //Checkt ob das Format eine Zertifikatnummer hat
var frm, nxtfrm, wrd, nxtwrd, cntr = 1, pcntr = 1;//Frame, Word, Counter & Pagecounter
switch (auswahlMz) { //absolute Position des Jahres in der Zertifikatnummer
case "HKN NEU100": sCN = 0; break;
case "HKN NEU100_engl": sCN = 1; break;
case "HKN NEU100 mit CO2-Einsparung": sCN = 2; break;
case "HKN NEU100_wind": sCN = 3; frm = 4, wrd = 6; break;
case "RenewablePLUS": sCN = 4; frm = 2, wrd = 5; break;
case "RenewablePLUS eng.": sCN = 5; frm = 3, wrd = 7; break;
case "RenewablePLUS mit CO2-Einsparung": sCN = 6; frm = 4, wrd = 5; break;
case "ÖkoPLUS": sCN = 7; frm = 2, wrd = 5; break;
case "ÖkoPLUS_mit_Einsparung": sCN = 8; frm = 3, wrd = 5; break;
case "ÖkoPLUS_mit_Einsparung_Engl": sCN = 9; frm = 6, wrd = 7; break;
case "RenewablePLUS REGIO_bäumt auf": sCN = 10; break;
case "RenewablePLUS REGIO_blüht auf": sCN = 11; break;
case "ÖkoPLUS REGIO_bäumt auf": sCN = 12; break;
case "ÖkoPLUS REGIO_blüht auf": sCN = 13; break;
case "HKN NEU100 REGIO_bäumt auf": sCN = 14; break;
case "HKN NEU100 REGIO_blüht auf": sCN = 15; break;
default: alert("Fehler");
}
if(auswahlAlt.value){frm = sortPosArr[sCN][0], wrd = sortPosArr[sCN][1];}
else if(auswahlNeu.value){frm = sortPosArr[sCN][2], wrd = sortPosArr[sCN][3];}
alert(sCN+" "+frm+" "+wrd);
if (document.pages[0].textFrames[frm].texts[0].words[wrd].contents === undefined || document.pages[0].textFrames[frm].texts[0].words[wrd].contents.length != 4) {
if (auswahlNeu.value) {
frm = sortPosArr[sCN][2];
wrd = sortPosArr[sCN][3];
}
else{
frm = sortPosArr[sCN][0];
wrd = sortPosArr[sCN][1];
}
}//Design von Page 0 wird überprüft und die Positionen angepasst
if (document.pages[0].textFrames[frm].texts[0].words[wrd].contents > stromMenge_jahrPnl.jahr.text) {
document.pages[numberOfPages - 1].move(LocationOptions.BEFORE, document.pages[0]);
pcntr = 0;
cntr = 1;
}//Jahr < erstes Jahr in Datei
Btw if (document.pages[0].textFrames[frm].texts[0].words[wrd].contents === undefined || document.pages[0].textFrames[frm].texts[0].words[wrd].contents.length != 4) is the line of failure and dont mind the double assignments on the cases, I wasnt done yet computing the positions

How can I write this code less and better in Laravel?

How can I write this code less and better in Laravel?
I am writing this code to remove the / r character in the input text and also to calculate the number of characters in the text.
$removeRchar = Str::remove("\r", $request['message']);
$length = Str::length($removeRchar);
if ($length <= 70) {
$pages = 1;
} else {
$pages = ceil($length / 70);
}
You could create a Stringable
$length = Str::of($request['message'])
->remove('\r')
->length();
$pages = ceil($length / 70);
If you don't want an extra page when you have a lenght of 70, 140 etc. you should add a check. For example:
$pages = $lenght % 70 === 0
? $lenght / 70
: ceil($length / 70);

Record counter by condition in one query

Is there a way to count the reviews by rating count before calling the get (-> get) method to get all those counters by querying the database without any calculations on the server? My solution at the moment:
$allReviews = Review::query()
->where('product_id', $data['product_id'])
->whereNotNull('published_at')
->get();
$fiveStars = count($allReviews->where('rating', 5));
$fourStars = count($allReviews->where('rating', 4));
$threeStars = count($allReviews->where('rating', 3));
$twoStars = count($allReviews->where('rating', 2));
$oneStar = count($allReviews->where('rating', 1));
$overallRating = ($fiveStars * 5 + $fourStars * 4 + $threeStars * 3 + $twoStars * 2 + $oneStar) / (($fiveStars + $fourStars + $threeStars + $twoStars + $oneStar));
You could use groupBy and pluck:
$ratings = Review::query()
->selectRaw('rating, COUNT(*) as amount')
->where('product_id', $data['product_id'])
->whereNotNull('published_at')
->groupBy('rating')
->pluck('amount', 'rating');
$fiveStars = $rating[5] ?? 0;
$fourStars = $rating[4] ?? 0;
$threeStars = $rating[3] ?? 0;
$twoStars = $rating[2] ?? 0;
$oneStar = $rating[1] ?? 0;

How to read the Base Framing Protocol of RFC6455?

My reference are:
Writing a WebSocket server in Java
Base Framing Protocol
Why the first byte 129 represent FIN, RSV1, RSV2, RSV3, and Opcode?
My expected result are:
The first byte is the FIN / 1 bit, RSV1 / 1 bit, RSV2 / 1 bit, RSV3 / 1 bit, Opcode / 1 bit, Mask / 1 bit. Total 9 bits.
The second byte is the Payload length. Total 7 bits.
My actual result are:
The first byte represent FIN, RSV1, RSV2, RSV3, and Opcode.
The second byte represent the Payload length.
Just to illustrate a bit.
First Byte:
Leftmost bit is the fin-bit rightmost 4 bits represents the opcode,
in this case 1=text
10000001
Second Byte:
Leftmost bit indicates if data is masked remaining seven indicate the length
10000000 here the lenght is zero
11111101 here the lenght is exactly 125
11111110 here the lenght indicator is 126 therefor the next two bytes will give you the length followed by four bytes for the mask-key
11111111 here the lenght indicator is 127 therefor the next eight bytes will give you the length followed by four bytes for the mask-key
After all this follows the masked payload.
ADDED 2021-07-19
To extract information like opcode and length, you have to apply some bit operations on the given bytes.
Below is an extraction from
https://github.com/napengam/phpWebSocketServer/blob/master/server/RFC6455.php to show how the server decodes a frame.
public function Decode($frame) {
// detect ping or pong frame, or fragments
$this->fin = ord($frame[0]) & 128;
$this->opcode = ord($frame[0]) & 15;
$length = ord($frame[1]) & 127;
if ($length <= 125) {
$moff = 2;
$poff = 6;
} else if ($length == 126) {
$l0 = ord($frame[2]) << 8;
$l1 = ord($frame[3]);
$length = ($l0 | $l1);
$moff = 4;
$poff = 8;
} else if ($length == 127) {
$l0 = ord($frame[2]) << 56;
$l1 = ord($frame[3]) << 48;
$l2 = ord($frame[4]) << 40;
$l3 = ord($frame[5]) << 32;
$l4 = ord($frame[6]) << 24;
$l5 = ord($frame[7]) << 16;
$l6 = ord($frame[8]) << 8;
$l7 = ord($frame[9]);
$length = ( $l0 | $l1 | $l2 | $l3 | $l4 | $l5 | $l6 | $l7);
$moff = 10;
$poff = 14;
}
$masks = substr($frame, $moff, 4);
$data = substr($frame, $poff, $length); // hgs 30.09.2016
$text = '';
$m0 = $masks[0];
$m1 = $masks[1];
$m2 = $masks[2];
$m3 = $masks[3];
for ($i = 0; $i < $length;) {
$text .= $data[$i++] ^ $m0;
if ($i < $length) {
$text .= $data[$i++] ^ $m1;
if ($i < $length) {
$text .= $data[$i++] ^ $m2;
if ($i < $length) {
$text .= $data[$i++] ^ $m3;
}
}
}
}
return $text;
}
In https://github.com/napengam/phpWebSocketServer/blob/master/phpClient/websocketCore.php you will find encode and decode for the client.

After Effects: Return value if variable equals part of a set of numbers

I'm writing an AE Expression that will spit out a number based on an Effect's keyframed value on the same layer. That is, if it's 1, value is 100, if it's 2, value is 101, if it's 3, 99, etc. Here's what I've got working:
x = effect("Mouth")("Slider");
if (x == 7 || x == 11 || x == 16) {
103
} else if (x == 6 || x == 10 || x == 15 || x == 25 || x == 26){
102
} else if (x == 5 || x == 9 || x == 12 || x == 14 || x == 19 || x == 24 || x == 27 || x == 28){
101
} else {
100
}
Surely there is a more elegant way to do this? I've tried writing it
if (x == 7 || 11 || 16)
but telling After Effects X absolutely equals "this" OR "that" just makes it assume it also equals "everything". Argh.
This is a little weird (arguably elegant?), but if you're just looking for something more compact, you could do:
x = effect("Mouth")("Slider");
if (",7,11,16,".indexOf(","+x+",") != -1) {
103
} else if (",6,10,15,25,26,".indexOf(","+x+",") != -1) {
102
} else if (",5,9,12,14,19,24,27,28,".indexOf(","+x+",") != -1) {
101
} else {
100
}
but don't forget the commas at both ends!
I thought about using ECMA's array.indexOf(x), but it is not supported in AE's JS expressions. [edit: mistakedly wrote 'offsetOf']
Here are two other approaches you could use. You can compress this code more, but I left it spread out for readability.
For shorter lists you can use a switch/case and have multiple options execute the same code. Like so:
thingToTest = effect("Mouth")("Slider");
switch (thingToTest) {
case 7:
case 11:
case 16:
result = 103;
break;
case 6:
case 10:
case 15:
case 25:
case 26:
result = 102;
break;
case 5:
case 9:
case 12:
case 14:
case 19:
case 24:
case 27:
case 28:
result = 101;
break;
default:
result = 100;
break;
}
The problem is that if you have a lot of possible outcomes to check for, that could become quite unwieldy. In which case, I'd make the values for each outcome case be an array and loop through them.
thingToTest = effect("Mouth")("Slider");
mySet1 = [7, 11, 16];
mySet2 = [6, 10, 15, 25, 26];
mySet3 = [5, 9, 12, 14, 19, 24, 27, 28];
result = 100; // Default
foundIt = 0;
for (i = 0; i < mySet1.length; i++) {
if (mySet1[i] == thingToTest) {
result = 103;
foundIt = 1;
break;
}
}
if(foundIt) {
for (i = 0; i < mySet2.length; i++) {
if (mySet2[i] == thingToTest) {
result = 102;
foundIt = 1;
break;
}
}
}
if(foundIt) {
for (i = 0; i < mySet3.length; i++) {
if (mySet3[i] == thingToTest) {
result = 101;
foundIt = 1;
break;
}
}
}
result;
Putting the successive groups in a conditional statement marginally improves performance by not iterating through those arrays if a match has already been found. For efficiency, it would make sense to test against the contents of your longest list of numbers first, because it's more likely to contain the match.
These solutions may not be as compact but are definitely scaleable.

Resources