Laravel 8 - 属性 [xxx] 在 Eloquent 构建器实例上不存在
Laravel 8 - Property [xxx] does not exist on the Eloquent builder instance
我在这里向您展示“只是另一个错误”。
我没有找到任何好的来源来解释为什么我会收到此错误。
所以,我有两个数据库 table,它们是 withdraws
和 users
我的表格效果理想,它几乎完全符合我的要求,在 withdraws
table 中创建记录,但是..不会在 users
中更改资金
表单中的 v-model 和名称参数是“数量”,但让我向您展示我在该控制器代码中的功能
WithdrawController.php
:
public function store(StoreRequest $request)
{
$data = $request->getData();
try {
DB::beginTransaction();
$this->withdrawService->create($data);
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
Log::error($e->getMessage());
return redirect()->back();
}
$user = User::where('id', auth()->user()->id);
$user->funds -= $data['amount'];
$user->save();
Mail::to('')->send(new WithdrawRequestMail($user, $data['amount']));
return redirect()->back();
}
您已创建查询生成器但尚未执行任何查询:
$user = User::where('id', auth()->user()->id);
然后您尝试像使用模型一样使用 $user
。您需要将 first()
添加到获得用户所需的内容中:
$user = User::where('id', auth()->user()->id)->first();
尽管您已经拥有来自 Auth 系统的用户:
$user = auth()->user();
我在这里向您展示“只是另一个错误”。 我没有找到任何好的来源来解释为什么我会收到此错误。
所以,我有两个数据库 table,它们是 withdraws
和 users
我的表格效果理想,它几乎完全符合我的要求,在 withdraws
table 中创建记录,但是..不会在 users
表单中的 v-model 和名称参数是“数量”,但让我向您展示我在该控制器代码中的功能
WithdrawController.php
:
public function store(StoreRequest $request)
{
$data = $request->getData();
try {
DB::beginTransaction();
$this->withdrawService->create($data);
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
Log::error($e->getMessage());
return redirect()->back();
}
$user = User::where('id', auth()->user()->id);
$user->funds -= $data['amount'];
$user->save();
Mail::to('')->send(new WithdrawRequestMail($user, $data['amount']));
return redirect()->back();
}
您已创建查询生成器但尚未执行任何查询:
$user = User::where('id', auth()->user()->id);
然后您尝试像使用模型一样使用 $user
。您需要将 first()
添加到获得用户所需的内容中:
$user = User::where('id', auth()->user()->id)->first();
尽管您已经拥有来自 Auth 系统的用户:
$user = auth()->user();