遇到格式错误的数值-购物车购物篮

时间:2019-01-13 18:21:23

标签: php laravel

打开通知时,我收到以下消息标记:

  

注意:遇到格式不正确的数值

https://i.stack.imgur.com/lcI4P.png

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
use App\Cart;
use App\orders;
use DB;
use Auth;
class CartController extends Controller
{
    public function add(){
        $id = request('id');
        $product = Product::find($id);
    $oldCart = session()->has('cart') ? session()->get('cart') : null;
    $cart = new Cart($oldCart);
    $cart->add($product, $product->id);
    request()->session()->put('cart', $cart);
    return response()->json($cart->totalQty);
}
// public function add22(){
//     $id = request('id');
//     $product = Product::find($id);
//     $oldCart = session()->has('cart2') ? session()->get('cart2') : null;
//     $cart = new Cart($oldCart);
//     $cart->add($product, $product->id);
//     request()->session()->put('cart2', $cart);
//     return response()->json($cart->totalQty);
// }
public function add2($id) 
{
    $product = Product::find($id);      
    $oldCart = session()->has('cart') ? session()->get('cart') : null;
    $cart = new Cart($oldCart);
    $cart->add($product, $product->id);
    request()->session()->put('cart', $cart);       
    // dd(session()->get('cart'));

    return back();
}
public function index(){
    if(session()->has('cart'))
    {
        $products = session()->get('cart')->items;
    }
    // elseif(session()->has('cart2'))
    // {
    //     $products = session()->get('cart2')->items;
    // }
    else
    {
        $products = 'nothing';
    }
    $currency = DB::table('currency')->get();
    return view('basket', compact('products', 'currency'));
}
public function order(){
    if(session()->has('cart'))
    {
        $products = session()->get('cart')->items;
    }
    // elseif(session()->has('cart2'))
    // {
    //     $products = session()->get('cart2')->items;
    // }
    else
    {
        $products = 'nothing';
    }
    $currency = DB::table('currency')->get();
    return view('order', compact('products', 'currency'));
    // return view('order');
}
public function store(Request $request){
    $this ->validate(request(), [
        'name'=>'required',
        'address'=>'required',
        'auth'=>'required',
    ]);
    if(Auth::check())
    {
    $orders = orders::create([
        'name' => $request['name'],
        'description' => $request['address'],
        'auth' => $request['auth'],
    ]);
    }
    $cart = session()->get('cart');
    $message = "Имя: ".request('name')."\nАдрес:".request('address')."\nЗаказанные товары:";
    foreach($cart->items as $product) {
        $message .= $product['item']['name']." * ".$product['qty'].", Цена - ".$product['item']['entityprice']*$product['qty']."\n";
    }
    $message .= "\nИтого: ".$cart->totalPrice." тг\n";
    $subject = "Заказ товара";     
    $headers = 'From: info@hansolomed.kz' . "\r\n" .
        'Reply-To: info@hansolome.kz' . "\r\n".
        'X-Mailer: PHP/' . phpversion();
    mail('015@i-marketing.kz', $subject, $message, $headers);
    request()->session()->forget('cart');
}    
public function remove($id){
    $oldCart = session()->has('cart') ? session()->get('cart') : null;
    $cart = new Cart($oldCart);
    $cart->removeItem($id);
    if(count($cart->items) > 0){
        session()->put('cart', $cart);
    }else{
        session()->forget('cart', $cart);
    }
    return redirect()->back();
}    
public function redis($id){
    $oldCart = session()->has('cart') ? session()->get('cart') : null;
    $cart = new Cart($oldCart);
    $cart->reduceByOne($id);
    session()->put('cart', $cart);
    return redirect()->back();        
}

}

3 个答案:

答案 0 :(得分:0)

我认为user3783243是正确的。使用floatval()或intval(),然后可以将它们相乘。

<?php echo e(number_format( floatval( session()->get(cart)->totalPrice, 0, ',',' ')* floatval($currency[0]->coefficient) ; ?>

答案 1 :(得分:0)

您正在使用number_format字符串进行计算,该字符串不会返回有效的格式化数字。

echo number_format(1000.95, 0, ',', ' ');

打印1 001

'1 001' * 2发出通知并计算2(1 * 2),而不是预期的2001.90

您的格式表示0小数点,,作为小数点,空格表示成千的分隔符。仅将数字格式用于输出,请勿使用带格式的数字进行计算。

您可能想要:

echo e(number_format( session()->get(cart)->totalPrice * $currency[0]->coefficient , 2, ',', '' ));

答案 2 :(得分:0)

对于备用https://github.com/bumbummen99/LaravelShoppingcart,它是https://github.com/Crinsane/LaravelShoppingcart的分支项目,还支持最新的Laravel版本。

有一个名为Cart::totalFloat()的方法,该方法将返回购物车总额的浮动值。

###############或################

对于此软件包,只需将数字格式转换回整数或浮点值即可。下面显示了一个示例

$ num ='1,200,998.255';

##########浮点数##########################

echo filter_var($num, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);

#output:1200998.255

##########的整数值##########################

echo filter_var($num, FILTER_SANITIZE_NUMBER_INT);

#output:1200998