php - 传递给 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;
}
这给了我以下(标题)错误
解决方案
你应该使用Auth::user()
而不是Auth()->user()
$this->submit_payment_wallet(Auth::user(), $price, $request->discount, $subject);}
推荐阅读
- sql - PostgreSQL 中 ANY(ARRAY[..]) 与 ANY(VALUES (), () ..) 之间的区别
- python - Python多处理超时是实时的还是计算时间的?
- c# - 如何在排序链表中实现用户输入的整数?
- java - Java - 即使 MANIFEST.MF 文件位于 /META-INF 中,App.jar 中也没有主要清单属性
- zip - Spring Batch 程序创建固定大小的 zip 文件
- reactjs - 如何在 Stack.screen 上设置参数化标题
- python - 熊猫数据框如何用多个替换单列
- android - 如何解决任务':app:checkDebugDuplicateClasses'的执行失败
- java - 与用户交谈文本+图像并将其保存到 firebase 以便稍后显示。(如法定人数)
- javascript - 需要 React Hook Form 动态