dongyue6199 2019-02-28 13:18
浏览 55
已采纳

获取laravel中生成的发票号

I'm trying to generate an invoice number, but I'm getting A non-numeric value encountered. I think I'm missing something but I'm not sure what it is.

Here is the invoiceNumber() code in my helpers.php

function invoiceNumber()
{
    $orders = App\Order::all();

    if($orders->isEmpty())
    {
        $invoice = 'arm0001';
        return $invoice;
    }

    foreach($orders as $order)
    {
        $latest = App\Order::latest()->first();

        if($latest->invoice_number == true)
        {
            $invoice = 'arm'.$latest->invoice_number+1;
            return $invoice;
        }
    }
}

and this is where I'm trying to get it to be taken

public function deliveryConfirmation()
{
    $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
    $contacts = Contact::all();

    if(!Session::has('cart'))
    {
        return view('public.shopping-cart', compact('menus_child', 'contacts'));
    }

    $oldCart = Session::get('cart');
    $cart = new Cart($oldCart);

    $invoice_number = invoiceNumber();
    dd($invoice_number);

    return view('public.delivery-confirmation', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice, 'menus_child' => $menus_child, 'contacts' => $contacts, 'invoice_number' => $invoice_number]);
}

I would really like to keep the 'ARM' in the database as well

  • 写回答

4条回答 默认 最新

  • douxiangdiao6348 2019-02-28 13:58
    关注

    So after seeing everyone's answer and from my understanding, it looked like everyone was separating the integer from the string to get it to work and it got me thinking about doing a preg_replace(). so I've adjusted my code to look like this

    function invoiceNumber()
    {
        $latest = App\Order::latest()->first();
    
        if (! $latest) {
            return 'arm0001';
        }
    
        $string = preg_replace("/[^0-9\.]/", '', $latest->invoice_number);
    
        return 'arm' . sprintf('%04d', $string+1);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化