I want to write this if..else statements with ternary operators:
if((($request->city)=="98")&&($cartPrice >= 300000)&&($request->province)=="8")){ "ort_free_delivery" => 'City sending free delivery' }elseif((($request->city)!="98")&&($cartPrice >=300000)&&($request->province)!="8")){ "ort_free_delivery" => 'Country sending free delivery' }else{ "ort_free_delivery" => '', }
And here is my try:
( ( ($request->city)=="98" ) ? ($cartPrice>=300000) ? ( ( ($request->province)=="8" ) ? "ort_free_delivery" == "City sending free delivery" ) ); ( ( ($request->city)!="98" ) ? ($cartPrice>=300000) ? ( ( ($request->province)!="8" ) ? "ort_free_delivery" == "Country sending free delivery" : "ort_free_delivery" == "" ) );
But this is wrong because there are two lines of ternary operators, and I need this condition in one line (because I’m inserting some data with DB class in Laravel):
DB::table('order_detail')->insert([ "ort_ord_id" => $orderId, "ort_amount" => $value->price, "ort_total" => $value->discounted * $value->quantity, "ort_discount" => ($value->price * $value->quantity) - $value->discounted * $value->quantity, "ort_type" => "product", "ort_number" => $value->quantity, "ort_reference_id" => $value->id, ( ( ($request->city)=="98" ) ? ($cartPrice>=300000) ? ( ( ($request->province)=="8" ) ? "ort_free_delivery" == "City sending free delivery" ) ) ( ( ($request->city)!="98" ) ? ($cartPrice>=300000) ? ( ( ($request->province)!="8" ) ? "ort_free_delivery" == "Country sending free delivery" : "ort_free_delivery" == "" ) ) "created_at" => now(), "updated_at" => now() ]);
So my question is how can I add this conditional statement with ternary operators in one line ?
UPDATE #1:
I tried this to check if the conditions work or not:
if ($foundOrder != null) { DB::table('order_detail')->whereOrtOrdId($orderId)->delete(); foreach ($foundOrder as $key => $value) { $existProduct = DB::table('order_detail')->whereOrtOrdId($orderId)->whereOrtReferenceId($value->id)->exists(); if (empty($existProduct)) { $price = Product::wherePrdId($value->id)->select('prd_sale_price')->first(); if($request->city==98&&$cartPrice >= 300000&&$request->province==8){ dd('city'); }elseif($request->city!=98&&$cartPrice >=300000&&$request->province!=8){ dd('country'); }else{ $data["ort_free_delivery"] = ''; } } } } else { DB::table('order_detail')->whereOrtOrdId($orderId)->delete(); }
And it properly shows city
and country
if their conditions return true,
Advertisement
Answer
One option is
$data=[ "ort_ord_id" => $orderId, "ort_amount" => $value->price, "ort_total" => $value->discounted * $value->quantity, "ort_discount" => ($value->price * $value->quantity) - $value->discounted * $value->quantity, "ort_type" => "product", "ort_number" => $value->quantity, "ort_reference_id" => $value->id, "created_at" => now(), "updated_at" => now() ]; if($request->city==98&&$cartPrice >= 300000&&$request->province==8){ $data["ort_free_delivery"] = 'City sending free delivery'; }elseif($request->city!=98&&$cartPrice >=300000&&$request->province!=8){ $data["ort_free_delivery"] = 'Country sending free delivery'; }else{ $data["ort_free_delivery"] = ''; }
Then query
DB::table('order_detail')->insert($data);
or
DB::table('order_detail')->insert([ "ort_ord_id" => $orderId, "ort_amount" => $value->price, "ort_total" => $value->discounted * $value->quantity, "ort_discount" => ($value->price * $value->quantity) - $value->discounted * $value->quantity, "ort_type" => "product", "ort_number" => $value->quantity, "ort_reference_id" => $value->id, "ort_free_delivery" =>($request->city==98&&$cartPrice >= 300000&&$request->province==8)?'City sending free delivery': (($request->city!=98&&$cartPrice >=300000&&$request->province!=8)?'Country sending free delivery':null), "created_at" => now(), "updated_at" => now() ]);