首页 > 解决方案 > 传递给 App\Http\Controllers\FrontEnd\paymentController::submit_payment_wallet() 的参数 1 必须是 App\Models\User 的实例,给定 null,

问题描述

我想用钱包完成买家付款。我怎么解决这个问题?这是我的代码:控制器:

use Payment, Buy;
  public function get_data_submit(Request $request)
{
    $request->validate([
        'discount' => 'nullable|string',
    ]);
    $price = Session()->get('payment_request.amount');
    $subject = Session()->get('payment_request.title');
    if ($request->type == 0)
        $res = $this->submit_payment_wallet(Auth()->user(), $price, $request->discount, $subject);}

我使用 trait 是因为 a 有更多的控制器:

    public function submit_payment_wallet(User $user, $price, $discount = null, $subject = null, invoice $invoice = null)
{
    $checked_discount = null;
    $final_price = $price;
    if ($discount !== null && $checked_discount = $this->check_discount($user, $price, $discount))
        $final_price = submitDiscount($price, $checked_discount->amount, $checked_discount->price);
    if ($user->wallet >= $final_price) {
        DB::transaction(function () use ($final_price, $price, $user, $checked_discount, $discount, $subject, $invoice) {
           
            if ($subject !== null) {
                $invoice_create = $user->invoice()->create([
                    'subject' => $subject,
                    'price' => $price,
                    'total_price' => $final_price,
                    'discount' => $checked_discount->amount ?? 0,
                    'status' => 1
                ]);
                $invoice_create->items()->create([
                    'title' => $subject,
                    'price' => $price
                ]);
            }
            if ($invoice !== null)
                $invoice->update([
                    'total_price' => $final_price,
                    'discount' => $checked_discount->amount ?? $invoice->discount,
                    'status' => 1
                ]);
            $user->update([
                'wallet' => $user->wallet - $final_price
            ]);

        });
        return true;
    }
    return false;
}

这给了我以下(标题)错误

标签: phplaravel

解决方案


你应该使用Auth::user()而不是Auth()->user()

$this->submit_payment_wallet(Auth::user(), $price, $request->discount, $subject);}

文档中所述


推荐阅读